Skip to content

Commit 4c60d04

Browse files
authored
feat: add solutions to lc problems: No.3142,3143 (doocs#2796)
* No.3142.Check if Grid Satisfies Conditions * No.3143.Maximum Points Inside the Square
1 parent da37d4c commit 4c60d04

File tree

31 files changed

+1297
-17
lines changed

31 files changed

+1297
-17
lines changed

solution/3100-3199/3142.Check if Grid Satisfies Conditions/README.md

+78-4
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,98 @@
7272

7373
## 解法
7474

75-
### 方法一
75+
### 方法一:模拟
76+
77+
我们可以遍历每一个格子,判断其是否满足题目条件,如果有一个格子不满足条件,我们就返回 `false`,否则返回 `true`
78+
79+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵 `grid` 的行数和列数。空间复杂度 $O(1)$。
7680

7781
<!-- tabs:start -->
7882

7983
```python
80-
84+
class Solution:
85+
def satisfiesConditions(self, grid: List[List[int]]) -> bool:
86+
m, n = len(grid), len(grid[0])
87+
for i, row in enumerate(grid):
88+
for j, x in enumerate(row):
89+
if i + 1 < m and x != grid[i + 1][j]:
90+
return False
91+
if j + 1 < n and x == grid[i][j + 1]:
92+
return False
93+
return True
8194
```
8295

8396
```java
84-
97+
class Solution {
98+
public boolean satisfiesConditions(int[][] grid) {
99+
int m = grid.length, n = grid[0].length;
100+
for (int i = 0; i < m; ++i) {
101+
for (int j = 0; j < n; ++j) {
102+
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
103+
return false;
104+
}
105+
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
106+
return false;
107+
}
108+
}
109+
}
110+
return true;
111+
}
112+
}
85113
```
86114

87115
```cpp
88-
116+
class Solution {
117+
public:
118+
bool satisfiesConditions(vector<vector<int>>& grid) {
119+
int m = grid.size(), n = grid[0].size();
120+
for (int i = 0; i < m; ++i) {
121+
for (int j = 0; j < n; ++j) {
122+
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
123+
return false;
124+
}
125+
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
126+
return false;
127+
}
128+
}
129+
}
130+
return true;
131+
}
132+
};
89133
```
90134
91135
```go
136+
func satisfiesConditions(grid [][]int) bool {
137+
m, n := len(grid), len(grid[0])
138+
for i, row := range grid {
139+
for j, x := range row {
140+
if i+1 < m && x != grid[i+1][j] {
141+
return false
142+
}
143+
if j+1 < n && x == grid[i][j+1] {
144+
return false
145+
}
146+
}
147+
}
148+
return true
149+
}
150+
```
92151

152+
```ts
153+
function satisfiesConditions(grid: number[][]): boolean {
154+
const [m, n] = [grid.length, grid[0].length];
155+
for (let i = 0; i < m; ++i) {
156+
for (let j = 0; j < n; ++j) {
157+
if (i + 1 < m && grid[i][j] !== grid[i + 1][j]) {
158+
return false;
159+
}
160+
if (j + 1 < n && grid[i][j] === grid[i][j + 1]) {
161+
return false;
162+
}
163+
}
164+
}
165+
return true;
166+
}
93167
```
94168

95169
<!-- tabs:end -->

solution/3100-3199/3142.Check if Grid Satisfies Conditions/README_EN.md

+78-4
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,98 @@
6868

6969
## Solutions
7070

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)`.
7276

7377
<!-- tabs:start -->
7478

7579
```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
7790
```
7891

7992
```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+
}
81109
```
82110

83111
```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+
};
85129
```
86130
87131
```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+
```
88147

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+
}
89163
```
90164

91165
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool satisfiesConditions(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
for (int i = 0; i < m; ++i) {
6+
for (int j = 0; j < n; ++j) {
7+
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
8+
return false;
9+
}
10+
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
11+
return false;
12+
}
13+
}
14+
}
15+
return true;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func satisfiesConditions(grid [][]int) bool {
2+
m, n := len(grid), len(grid[0])
3+
for i, row := range grid {
4+
for j, x := range row {
5+
if i+1 < m && x != grid[i+1][j] {
6+
return false
7+
}
8+
if j+1 < n && x == grid[i][j+1] {
9+
return false
10+
}
11+
}
12+
}
13+
return true
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean satisfiesConditions(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
for (int i = 0; i < m; ++i) {
5+
for (int j = 0; j < n; ++j) {
6+
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
7+
return false;
8+
}
9+
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
10+
return false;
11+
}
12+
}
13+
}
14+
return true;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def satisfiesConditions(self, grid: List[List[int]]) -> bool:
3+
m, n = len(grid), len(grid[0])
4+
for i, row in enumerate(grid):
5+
for j, x in enumerate(row):
6+
if i + 1 < m and x != grid[i + 1][j]:
7+
return False
8+
if j + 1 < n and x == grid[i][j + 1]:
9+
return False
10+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function satisfiesConditions(grid: number[][]): boolean {
2+
const [m, n] = [grid.length, grid[0].length];
3+
for (let i = 0; i < m; ++i) {
4+
for (let j = 0; j < n; ++j) {
5+
if (i + 1 < m && grid[i][j] !== grid[i + 1][j]) {
6+
return false;
7+
}
8+
if (j + 1 < n && grid[i][j] === grid[i][j + 1]) {
9+
return false;
10+
}
11+
}
12+
}
13+
return true;
14+
}

0 commit comments

Comments
 (0)