[LeetCode] Check if Grid Satisfies Conditions

3142. Check if Grid Satisfies Conditions

You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is:

  • Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists).
  • Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists).

Return true if all the cells satisfy these conditions, otherwise, return false.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool satisfiesConditions(vector<vector<int>>& A) {
for(int i = 0; i < A[0].size() - 1; i++) {
if(A[0][i] == A[0][i+1]) return false;
}
for(int i = 0; i < A.size() - 1; i++) {
if(A[i] != A[i+1]) return false;
}
return true;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/12/PS/LeetCode/check-if-grid-satisfies-conditions/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.