Skip to content

Commit 1af94b6

Browse files
committed
feat: update solutions to lc problem: No.2290
No.2290.Minimum Obstacle Removal to Reach Corner
1 parent 2da7999 commit 1af94b6

File tree

8 files changed

+112
-108
lines changed

8 files changed

+112
-108
lines changed

solution/1800-1899/1814.Count Nice Pairs in an Array/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
**方法一:式子变换 + 哈希表**
4949

50-
对于下标对 $(i, j)$,如果满足条件,那么有 $nums[i] + rev(nums[j]) = nums[j] + rev(nums[i])$,即 $nums[i] - rev(nums[i]) = nums[j] - rev(nums[j])$。移项后,有 $nums[i] - nums[j] = rev(nums[j]) - rev(nums[i])$。
50+
对于下标对 $(i, j)$,如果满足条件,那么有 $nums[i] + rev(nums[j]) = nums[j] + rev(nums[i])$,即 $nums[i] - nums[j] = rev(nums[j]) - rev(nums[i])$。
5151

5252
因此,我们可以将 $nums[i] - rev(nums[i])$ 作为哈希表的键,统计每个键出现的次数。最后计算每个键对应的值的组合数,相加得到最终的答案。
5353

@@ -194,8 +194,7 @@ public:
194194
const int mod = 1e9 + 7;
195195
for (int& x : nums) {
196196
int y = x - rev(x);
197-
ans = (ans + cnt[y]) % mod;
198-
cnt[y]++;
197+
ans = (ans + cnt[y]++) % mod;
199198
}
200199
return ans;
201200
}

solution/1800-1899/1814.Count Nice Pairs in an Array/README_EN.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ public:
176176
const int mod = 1e9 + 7;
177177
for (int& x : nums) {
178178
int y = x - rev(x);
179-
ans = (ans + cnt[y]) % mod;
180-
cnt[y]++;
179+
ans = (ans + cnt[y]++) % mod;
181180
}
182181
return ans;
183182
}

solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/README.md

+38-36
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262

6363
本题实际上也是最短路模型,只不过求解的是移除障碍物的最小数目。
6464

65-
在一个边权只有 $0$$1$ 的无向图中搜索最短路径可以使用双端队列进行 $BFS$。其原理是当前可以扩展到的点的权重为 $0$ 时,将其加入队首;权重为 $1$ 时,将其加入队尾。
65+
在一个边权只有 $0$, $1$ 的无向图中搜索最短路径可以使用双端队列进行 $BFS$。其原理是当前可以扩展到的点的权重为 $0$ 时,将其加入队首;权重为 $1$ 时,将其加入队尾。
6666

6767
> 如果某条边权值为 $0$,那么新拓展出的节点权值就和当前队首节点权值相同,显然可以作为下一次拓展的起点。
6868
69-
时间复杂度 $O(m*n)$,其中 $m$ 表示 $grid$ 行数,$n$ 表示 $grid$ 列数
69+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是网格的行数和列数
7070

7171
相似题目:[1368. 使网格图至少有一条有效路径的最小代价](/solution/1300-1399/1368.Minimum%20Cost%20to%20Make%20at%20Least%20One%20Valid%20Path%20in%20a%20Grid/README.md)
7272

@@ -79,24 +79,24 @@
7979
```python
8080
class Solution:
8181
def minimumObstacles(self, grid: List[List[int]]) -> int:
82-
q = deque([(0, 0, 0)])
8382
m, n = len(grid), len(grid[0])
83+
q = deque([(0, 0, 0)])
8484
vis = set()
85-
while q:
85+
dirs = (-1, 0, 1, 0, -1)
86+
while 1:
8687
i, j, k = q.popleft()
8788
if i == m - 1 and j == n - 1:
8889
return k
8990
if (i, j) in vis:
9091
continue
9192
vis.add((i, j))
92-
for a, b in ((0, -1), (0, 1), (-1, 0), (1, 0)):
93+
for a, b in pairwise(dirs):
9394
x, y = i + a, j + b
9495
if 0 <= x < m and 0 <= y < n:
9596
if grid[x][y] == 0:
9697
q.appendleft((x, y, k))
9798
else:
9899
q.append((x, y, k + 1))
99-
return 0
100100
```
101101

102102
### **Java**
@@ -106,14 +106,13 @@ class Solution:
106106
```java
107107
class Solution {
108108
public int minimumObstacles(int[][] grid) {
109-
int m = grid.length;
110-
int n = grid[0].length;
109+
int m = grid.length, n = grid[0].length;
111110
Deque<int[]> q = new ArrayDeque<>();
112111
q.offer(new int[] {0, 0, 0});
113112
int[] dirs = {-1, 0, 1, 0, -1};
114113
boolean[][] vis = new boolean[m][n];
115-
while (!q.isEmpty()) {
116-
int[] p = q.poll();
114+
while (true) {
115+
var p = q.poll();
117116
int i = p[0], j = p[1], k = p[2];
118117
if (i == m - 1 && j == n - 1) {
119118
return k;
@@ -122,19 +121,17 @@ class Solution {
122121
continue;
123122
}
124123
vis[i][j] = true;
125-
for (int o = 0; o < 4; ++o) {
126-
int x = i + dirs[o], y = j + dirs[o + 1];
124+
for (int h = 0; h < 4; ++h) {
125+
int x = i + dirs[h], y = j + dirs[h + 1];
127126
if (x >= 0 && x < m && y >= 0 && y < n) {
128127
if (grid[x][y] == 0) {
129128
q.offerFirst(new int[] {x, y, k});
130-
}
131-
if (grid[x][y] == 1) {
129+
} else {
132130
q.offerLast(new int[] {x, y, k + 1});
133131
}
134132
}
135133
}
136134
}
137-
return 0;
138135
}
139136
}
140137
```
@@ -146,26 +143,31 @@ class Solution {
146143
public:
147144
int minimumObstacles(vector<vector<int>>& grid) {
148145
int m = grid.size(), n = grid[0].size();
149-
deque<tuple<int, int, int>> q {{0, 0, 0}};
150-
vector<vector<bool>> vis(m, vector<bool>(n));
151-
vector<int> dirs = {-1, 0, 1, 0, -1};
152-
while (!q.empty()) {
146+
deque<tuple<int, int, int>> q{{0, 0, 0}};
147+
bool vis[m][n];
148+
memset(vis, 0, sizeof vis);
149+
int dirs[5] = {-1, 0, 1, 0, -1};
150+
while (1) {
153151
auto [i, j, k] = q.front();
154152
q.pop_front();
155-
if (i == m - 1 && j == n - 1) return k;
156-
if (vis[i][j]) continue;
153+
if (i == m - 1 && j == n - 1) {
154+
return k;
155+
}
156+
if (vis[i][j]) {
157+
continue;
158+
}
157159
vis[i][j] = true;
158-
for (int o = 0; o < 4; ++o) {
159-
int x = i + dirs[o], y = j + dirs[o + 1];
160+
for (int h = 0; h < 4; ++h) {
161+
int x = i + dirs[h], y = j + dirs[h + 1];
160162
if (x >= 0 && x < m && y >= 0 && y < n) {
161-
if (grid[x][y] == 0)
163+
if (grid[x][y] == 0) {
162164
q.push_front({x, y, k});
163-
else
165+
} else {
164166
q.push_back({x, y, k + 1});
167+
}
165168
}
166169
}
167170
}
168-
return 0;
169171
}
170172
};
171173
```
@@ -176,36 +178,36 @@ public:
176178
func minimumObstacles(grid [][]int) int {
177179
m, n := len(grid), len(grid[0])
178180
q := doublylinkedlist.New()
179-
q.Add([]int{0, 0, 0})
181+
type tuple struct{ i, j, k int }
182+
q.Add(tuple{0, 0, 0})
180183
vis := make([][]bool, m)
181184
for i := range vis {
182185
vis[i] = make([]bool, n)
183186
}
184-
dirs := []int{-1, 0, 1, 0, -1}
185-
for !q.Empty() {
187+
dirs := [5]int{-1, 0, 1, 0, -1}
188+
for {
186189
v, _ := q.Get(0)
187-
p := v.([]int)
190+
p := v.(tuple)
188191
q.Remove(0)
189-
i, j, k := p[0], p[1], p[2]
192+
i, j, k := p.i, p.j, p.k
190193
if i == m-1 && j == n-1 {
191194
return k
192195
}
193196
if vis[i][j] {
194197
continue
195198
}
196199
vis[i][j] = true
197-
for o := 0; o < 4; o++ {
198-
x, y := i+dirs[o], j+dirs[o+1]
200+
for h := 0; h < 4; h++ {
201+
x, y := i+dirs[h], j+dirs[h+1]
199202
if x >= 0 && x < m && y >= 0 && y < n {
200203
if grid[x][y] == 0 {
201-
q.Insert(0, []int{x, y, k})
204+
q.Insert(0, tuple{x, y, k})
202205
} else {
203-
q.Add([]int{x, y, k + 1})
206+
q.Add(tuple{x, y, k + 1})
204207
}
205208
}
206209
}
207210
}
208-
return 0
209211
}
210212
```
211213

solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/README_EN.md

+36-34
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,38 @@ Note that there may be other ways to remove 2 obstacles to create a path.
5555
```python
5656
class Solution:
5757
def minimumObstacles(self, grid: List[List[int]]) -> int:
58-
q = deque([(0, 0, 0)])
5958
m, n = len(grid), len(grid[0])
59+
q = deque([(0, 0, 0)])
6060
vis = set()
61-
while q:
61+
dirs = (-1, 0, 1, 0, -1)
62+
while 1:
6263
i, j, k = q.popleft()
6364
if i == m - 1 and j == n - 1:
6465
return k
6566
if (i, j) in vis:
6667
continue
6768
vis.add((i, j))
68-
for a, b in ((0, -1), (0, 1), (-1, 0), (1, 0)):
69+
for a, b in pairwise(dirs):
6970
x, y = i + a, j + b
7071
if 0 <= x < m and 0 <= y < n:
7172
if grid[x][y] == 0:
7273
q.appendleft((x, y, k))
7374
else:
7475
q.append((x, y, k + 1))
75-
return 0
7676
```
7777

7878
### **Java**
7979

8080
```java
8181
class Solution {
8282
public int minimumObstacles(int[][] grid) {
83-
int m = grid.length;
84-
int n = grid[0].length;
83+
int m = grid.length, n = grid[0].length;
8584
Deque<int[]> q = new ArrayDeque<>();
8685
q.offer(new int[] {0, 0, 0});
8786
int[] dirs = {-1, 0, 1, 0, -1};
8887
boolean[][] vis = new boolean[m][n];
89-
while (!q.isEmpty()) {
90-
int[] p = q.poll();
88+
while (true) {
89+
var p = q.poll();
9190
int i = p[0], j = p[1], k = p[2];
9291
if (i == m - 1 && j == n - 1) {
9392
return k;
@@ -96,19 +95,17 @@ class Solution {
9695
continue;
9796
}
9897
vis[i][j] = true;
99-
for (int o = 0; o < 4; ++o) {
100-
int x = i + dirs[o], y = j + dirs[o + 1];
98+
for (int h = 0; h < 4; ++h) {
99+
int x = i + dirs[h], y = j + dirs[h + 1];
101100
if (x >= 0 && x < m && y >= 0 && y < n) {
102101
if (grid[x][y] == 0) {
103102
q.offerFirst(new int[] {x, y, k});
104-
}
105-
if (grid[x][y] == 1) {
103+
} else {
106104
q.offerLast(new int[] {x, y, k + 1});
107105
}
108106
}
109107
}
110108
}
111-
return 0;
112109
}
113110
}
114111
```
@@ -120,26 +117,31 @@ class Solution {
120117
public:
121118
int minimumObstacles(vector<vector<int>>& grid) {
122119
int m = grid.size(), n = grid[0].size();
123-
deque<tuple<int, int, int>> q {{0, 0, 0}};
124-
vector<vector<bool>> vis(m, vector<bool>(n));
125-
vector<int> dirs = {-1, 0, 1, 0, -1};
126-
while (!q.empty()) {
120+
deque<tuple<int, int, int>> q{{0, 0, 0}};
121+
bool vis[m][n];
122+
memset(vis, 0, sizeof vis);
123+
int dirs[5] = {-1, 0, 1, 0, -1};
124+
while (1) {
127125
auto [i, j, k] = q.front();
128126
q.pop_front();
129-
if (i == m - 1 && j == n - 1) return k;
130-
if (vis[i][j]) continue;
127+
if (i == m - 1 && j == n - 1) {
128+
return k;
129+
}
130+
if (vis[i][j]) {
131+
continue;
132+
}
131133
vis[i][j] = true;
132-
for (int o = 0; o < 4; ++o) {
133-
int x = i + dirs[o], y = j + dirs[o + 1];
134+
for (int h = 0; h < 4; ++h) {
135+
int x = i + dirs[h], y = j + dirs[h + 1];
134136
if (x >= 0 && x < m && y >= 0 && y < n) {
135-
if (grid[x][y] == 0)
137+
if (grid[x][y] == 0) {
136138
q.push_front({x, y, k});
137-
else
139+
} else {
138140
q.push_back({x, y, k + 1});
141+
}
139142
}
140143
}
141144
}
142-
return 0;
143145
}
144146
};
145147
```
@@ -150,36 +152,36 @@ public:
150152
func minimumObstacles(grid [][]int) int {
151153
m, n := len(grid), len(grid[0])
152154
q := doublylinkedlist.New()
153-
q.Add([]int{0, 0, 0})
155+
type tuple struct{ i, j, k int }
156+
q.Add(tuple{0, 0, 0})
154157
vis := make([][]bool, m)
155158
for i := range vis {
156159
vis[i] = make([]bool, n)
157160
}
158-
dirs := []int{-1, 0, 1, 0, -1}
159-
for !q.Empty() {
161+
dirs := [5]int{-1, 0, 1, 0, -1}
162+
for {
160163
v, _ := q.Get(0)
161-
p := v.([]int)
164+
p := v.(tuple)
162165
q.Remove(0)
163-
i, j, k := p[0], p[1], p[2]
166+
i, j, k := p.i, p.j, p.k
164167
if i == m-1 && j == n-1 {
165168
return k
166169
}
167170
if vis[i][j] {
168171
continue
169172
}
170173
vis[i][j] = true
171-
for o := 0; o < 4; o++ {
172-
x, y := i+dirs[o], j+dirs[o+1]
174+
for h := 0; h < 4; h++ {
175+
x, y := i+dirs[h], j+dirs[h+1]
173176
if x >= 0 && x < m && y >= 0 && y < n {
174177
if grid[x][y] == 0 {
175-
q.Insert(0, []int{x, y, k})
178+
q.Insert(0, tuple{x, y, k})
176179
} else {
177-
q.Add([]int{x, y, k + 1})
180+
q.Add(tuple{x, y, k + 1})
178181
}
179182
}
180183
}
181184
}
182-
return 0
183185
}
184186
```
185187

0 commit comments

Comments
 (0)