Skip to content

Commit 70f66e2

Browse files
committed
Add solution and test-cases for problem 2290
1 parent dbd4dda commit 70f66e2

File tree

5 files changed

+85
-22
lines changed

5 files changed

+85
-22
lines changed
13.1 KB
Loading
8.79 KB
Loading

leetcode/2201-2300/2290.Minimum-Obstacle-Removal-to-Reach-Corner/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [2290.Minimum Obstacle Removal to Reach Corner][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** 2D integer array `grid` of size `m x n`. Each cell has one of two values:
5+
6+
- `0` represents an **empty** cell,
7+
- `1` represents an **obstacle** that may be removed.
8+
9+
You can move up, down, left, or right from and to an empty cell.
10+
11+
Return the **minimum** number of **obstacles** to **remove** so you can move from the upper left corner `(0, 0)` to the lower right corner `(m - 1, n - 1)`.
712

8-
**Example 1:**
13+
**Example 1:**
14+
15+
![1](./1.png)
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: grid = [[0,1,1],[1,1,0],[1,1,0]]
19+
Output: 2
20+
Explanation: We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).
21+
It can be shown that we need to remove at least 2 obstacles, so we return 2.
22+
Note that there may be other ways to remove 2 obstacles to create a path.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
27+
![2](./2.png)
1928

20-
### 思路1
21-
> ...
22-
Minimum Obstacle Removal to Reach Corner
23-
```go
2429
```
25-
30+
Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
31+
Output: 0
32+
Explanation: We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
33+
```
2634

2735
## 结语
2836

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type heap2290 [][3]int
6+
7+
func (h *heap2290) Len() int {
8+
return len(*h)
9+
}
10+
11+
func (h *heap2290) Swap(i, j int) {
12+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
13+
}
14+
15+
func (h *heap2290) Less(i, j int) bool {
16+
return (*h)[i][2] < (*h)[j][2]
17+
}
18+
19+
func (h *heap2290) Push(x any) {
20+
*h = append(*h, x.([3]int))
21+
}
22+
23+
func (h *heap2290) Pop() any {
24+
old := *h
25+
l := len(old)
26+
x := old[l-1]
27+
*h = old[:l-1]
428
return x
529
}
30+
31+
var dirs2290 = [][2]int{
32+
{0, 1}, {1, 0}, {-1, 0}, {0, -1},
33+
}
34+
35+
func Solution(grid [][]int) int {
36+
m, n := len(grid), len(grid[0])
37+
h := heap2290{{0, 0, 0}}
38+
v := map[[2]int]int{
39+
[2]int{0, 0}: 0,
40+
}
41+
for h.Len() > 0 {
42+
cur := heap.Pop(&h).([3]int)
43+
if cur[0] == m-1 && cur[1] == n-1 {
44+
return cur[2]
45+
}
46+
for _, dir := range dirs2290 {
47+
nx, ny := cur[0]+dir[0], cur[1]+dir[1]
48+
if !(nx >= 0 && nx < m && ny >= 0 && ny < n) {
49+
continue
50+
}
51+
need := cur[2] + grid[nx][ny]
52+
key := [2]int{nx, ny}
53+
//key = [3]int{nx, ny, need}
54+
if vv, ok := v[key]; !ok || vv > need {
55+
heap.Push(&h, [3]int{nx, ny, need})
56+
v[key] = need
57+
}
58+
}
59+
}
60+
return -1
61+
}

leetcode/2201-2300/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{0, 1, 1}, {1, 1, 0}, {1, 1, 0}}, 2},
17+
{"TestCase2", [][]int{{0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 1, 0}}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)