Check if Grid Satisfies Conditions
EASYDescription
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.
Example 1:
Input: grid = [[1,0,2],[1,0,2]]
Output: true
Explanation:

All the cells in the grid satisfy the conditions.
Example 2:
Input: grid = [[1,1,1],[0,0,0]]
Output: false
Explanation:

All cells in the first row are equal.
Example 3:
Input: grid = [[1],[2],[3]]
Output: false
Explanation:

Cells in the first column have different values.
Constraints:
1 <= n, m <= 100 <= grid[i][j] <= 9
Approaches
Checkout 2 different approaches to solve Check if Grid Satisfies Conditions. Click on different approaches to view the approach and algorithm in detail.
Single-Pass Iteration
This is the most efficient approach. It involves iterating through the grid just once. For each cell, it checks both the vertical and horizontal conditions simultaneously. If any condition is violated at any point, the function immediately returns false, which avoids unnecessary checks.
Algorithm
- Get the dimensions of the grid,
m(rows) andn(columns). - Iterate through each row
ifrom0tom-1. - Iterate through each column
jfrom0ton-1. - For the current cell
grid[i][j]:- If a cell below exists (
i < m - 1), check ifgrid[i][j] != grid[i+1][j]. If true, returnfalse. - If a cell to the right exists (
j < n - 1), check ifgrid[i][j] == grid[i][j+1]. If true, returnfalse.
- If a cell below exists (
- If the loops complete without returning
false, it means all conditions are satisfied. Returntrue.
The algorithm iterates through every cell of the grid using nested loops, with the outer loop for rows (i) and the inner loop for columns (j).
For each cell grid[i][j], it performs two checks:
- Vertical Condition: It checks if the cell is equal to the one below it. This check is only necessary if there is a cell below, i.e.,
i < m - 1. Ifgrid[i][j] != grid[i + 1][j], the condition is violated, and the function returnsfalse. - Horizontal Condition: It checks if the cell is different from the one to its right. This check is only necessary if there is a cell to the right, i.e.,
j < n - 1. Ifgrid[i][j] == grid[i][j + 1], the condition is violated, and the function returnsfalse. These checks are performed for each cell. The boundary conditions (i < m - 1andj < n - 1) are crucial to prevent out-of-bounds errors when accessinggrid[i + 1][j]andgrid[i][j + 1]. If the loops complete without finding any violations, it means all cells in the grid satisfy the given conditions, and the function returnstrue.
class Solution {
public boolean satisfiesConditions(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// Check condition 1: Equal to the cell below
if (i < m - 1) {
if (grid[i][j] != grid[i + 1][j]) {
return false;
}
}
// Check condition 2: Different from the cell to its right
if (j < n - 1) {
if (grid[i][j] == grid[i][j + 1]) {
return false;
}
}
}
}
return true;
}
}
Complexity Analysis
Pros and Cons
- This is the most time-efficient approach as it combines both checks into a single pass.
- It supports 'early exit', meaning it terminates as soon as a condition is violated, which can save significant time on large grids where a violation occurs early.
- The logic within the loop is slightly more complex due to handling two conditions and their respective boundary checks simultaneously.
Code Solutions
Checking out 3 solutions in different languages for Check if Grid Satisfies Conditions. Click on different languages to view the code.
class Solution {
public
boolean satisfiesConditions(int[][] grid) {
int m = grid.length, n = grid[0].length;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
return false;
}
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
return false;
}
}
}
return true;
}
}
Video Solution
Watch the video walkthrough for Check if Grid Satisfies Conditions
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.