|
68 | 68 |
|
69 | 69 | ## Solutions
|
70 | 70 |
|
71 |
| -### Solution 1 |
| 71 | +### Solution 1: Simulation |
| 72 | + |
| 73 | +We can iterate through each cell and determine whether it meets the conditions specified in the problem. If there is a cell that does not meet the conditions, we return `false`, otherwise, we return `true`. |
| 74 | + |
| 75 | +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix `grid` respectively. The space complexity is $O(1)`. |
72 | 76 |
|
73 | 77 | <!-- tabs:start -->
|
74 | 78 |
|
75 | 79 | ```python
|
76 |
| - |
| 80 | +class Solution: |
| 81 | + def satisfiesConditions(self, grid: List[List[int]]) -> bool: |
| 82 | + m, n = len(grid), len(grid[0]) |
| 83 | + for i, row in enumerate(grid): |
| 84 | + for j, x in enumerate(row): |
| 85 | + if i + 1 < m and x != grid[i + 1][j]: |
| 86 | + return False |
| 87 | + if j + 1 < n and x == grid[i][j + 1]: |
| 88 | + return False |
| 89 | + return True |
77 | 90 | ```
|
78 | 91 |
|
79 | 92 | ```java
|
80 |
| - |
| 93 | +class Solution { |
| 94 | + public boolean satisfiesConditions(int[][] grid) { |
| 95 | + int m = grid.length, n = grid[0].length; |
| 96 | + for (int i = 0; i < m; ++i) { |
| 97 | + for (int j = 0; j < n; ++j) { |
| 98 | + if (i + 1 < m && grid[i][j] != grid[i + 1][j]) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + if (j + 1 < n && grid[i][j] == grid[i][j + 1]) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return true; |
| 107 | + } |
| 108 | +} |
81 | 109 | ```
|
82 | 110 |
|
83 | 111 | ```cpp
|
84 |
| - |
| 112 | +class Solution { |
| 113 | +public: |
| 114 | + bool satisfiesConditions(vector<vector<int>>& grid) { |
| 115 | + int m = grid.size(), n = grid[0].size(); |
| 116 | + for (int i = 0; i < m; ++i) { |
| 117 | + for (int j = 0; j < n; ++j) { |
| 118 | + if (i + 1 < m && grid[i][j] != grid[i + 1][j]) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + if (j + 1 < n && grid[i][j] == grid[i][j + 1]) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | +}; |
85 | 129 | ```
|
86 | 130 |
|
87 | 131 | ```go
|
| 132 | +func satisfiesConditions(grid [][]int) bool { |
| 133 | + m, n := len(grid), len(grid[0]) |
| 134 | + for i, row := range grid { |
| 135 | + for j, x := range row { |
| 136 | + if i+1 < m && x != grid[i+1][j] { |
| 137 | + return false |
| 138 | + } |
| 139 | + if j+1 < n && x == grid[i][j+1] { |
| 140 | + return false |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return true |
| 145 | +} |
| 146 | +``` |
88 | 147 |
|
| 148 | +```ts |
| 149 | +function satisfiesConditions(grid: number[][]): boolean { |
| 150 | + const [m, n] = [grid.length, grid[0].length]; |
| 151 | + for (let i = 0; i < m; ++i) { |
| 152 | + for (let j = 0; j < n; ++j) { |
| 153 | + if (i + 1 < m && grid[i][j] !== grid[i + 1][j]) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + if (j + 1 < n && grid[i][j] === grid[i][j + 1]) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + return true; |
| 162 | +} |
89 | 163 | ```
|
90 | 164 |
|
91 | 165 | <!-- tabs:end -->
|
|
0 commit comments