LeetCode in Kotlin

3142. Check if Grid Satisfies Conditions

Easy

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

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:

Solution

class Solution {
    fun satisfiesConditions(grid: Array<IntArray>): Boolean {
        val m = grid.size
        val n = grid[0].size
        for (i in 0 until m - 1) {
            if (n > 1) {
                for (j in 0 until n - 1) {
                    if ((grid[i][j] != grid[i + 1][j]) || (grid[i][j] == grid[i][j + 1])) {
                        return false
                    }
                }
            } else {
                if (grid[i][0] != grid[i + 1][0]) {
                    return false
                }
            }
        }
        for (j in 0 until n - 1) {
            if (grid[m - 1][j] == grid[m - 1][j + 1]) {
                return false
            }
        }
        return true
    }
}