3142. Check if Grid Satisfies Conditions LeetCode Solution

In this guide, you will get 3142. Check if Grid Satisfies Conditions LeetCode Solution with the best time and space complexity. The solution to Check if Grid Satisfies Conditions problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Grid Satisfies Conditions solution in C++
  4. Check if Grid Satisfies Conditions solution in Java
  5. Check if Grid Satisfies Conditions solution in Python
  6. Additional Resources
3142. Check if Grid Satisfies Conditions LeetCode Solution image

Problem Statement of 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.

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 <= 10
0 <= grid[i][j] <= 9

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

3142. Check if Grid Satisfies Conditions LeetCode Solution in C++

class Solution {
 public:
  bool satisfiesConditions(vector<vector<int>>& grid) {
    const int m = grid.size();
    const int n = grid[0].size();

    for (int i = 0; i + 1 < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j] != grid[i + 1][j])
          return false;

    for (int i = 0; i < m; ++i)
      for (int j = 0; j + 1 < n; ++j)
        if (grid[i][j] == grid[i][j + 1])
          return false;

    return true;
  }
};
/* code provided by PROGIEZ */

3142. Check if Grid Satisfies Conditions LeetCode Solution in Java

class Solution {
  public boolean satisfiesConditions(int[][] grid) {
    final int m = grid.length;
    final int n = grid[0].length;

    for (int i = 0; i + 1 < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j] != grid[i + 1][j])
          return false;

    for (int i = 0; i < m; ++i)
      for (int j = 0; j + 1 < n; ++j)
        if (grid[i][j] == grid[i][j + 1])
          return false;

    return true;
  }
};
// code provided by PROGIEZ

3142. Check if Grid Satisfies Conditions LeetCode Solution in Python

class Solution:
  def satisfiesConditions(self, grid: list[list[int]]) -> bool:
    m = len(grid)
    n = len(grid[0])
    return (all(grid[i][j] == grid[i + 1][j]
                for i in range(m - 1)
                for j in range(n)) and
            all(grid[i][j] != grid[i][j + 1]
                for i in range(m)
                for j in range(n - 1)))
# code by PROGIEZ

Additional Resources

See also  300. Longest Increasing Subsequence LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.