Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [2290.Minimum Obstacle Removal to Reach Corner][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a **0-indexed** 2D integer array `grid` of size `m x n`. Each cell has one of two values:

- `0` represents an **empty** cell,
- `1` represents an **obstacle** that may be removed.

You can move up, down, left, or right from and to an empty cell.

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

**Example 1:**
**Example 1:**

![1](./1.png)

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

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Minimum Obstacle Removal to Reach Corner
```go
```

Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]
Output: 0
Explanation: We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
package Solution

func Solution(x bool) bool {
import "container/heap"

type heap2290 [][3]int

func (h *heap2290) Len() int {
return len(*h)
}

func (h *heap2290) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}

func (h *heap2290) Less(i, j int) bool {
return (*h)[i][2] < (*h)[j][2]
}

func (h *heap2290) Push(x any) {
*h = append(*h, x.([3]int))
}

func (h *heap2290) Pop() any {
old := *h
l := len(old)
x := old[l-1]
*h = old[:l-1]
return x
}

var dirs2290 = [][2]int{
{0, 1}, {1, 0}, {-1, 0}, {0, -1},
}

func Solution(grid [][]int) int {
m, n := len(grid), len(grid[0])
h := heap2290{{0, 0, 0}}
v := map[[2]int]int{
[2]int{0, 0}: 0,
}
for h.Len() > 0 {
cur := heap.Pop(&h).([3]int)
if cur[0] == m-1 && cur[1] == n-1 {
return cur[2]
}
for _, dir := range dirs2290 {
nx, ny := cur[0]+dir[0], cur[1]+dir[1]
if !(nx >= 0 && nx < m && ny >= 0 && ny < n) {
continue
}
need := cur[2] + grid[nx][ny]
key := [2]int{nx, ny}
//key = [3]int{nx, ny, need}
if vv, ok := v[key]; !ok || vv > need {
heap.Push(&h, [3]int{nx, ny, need})
v[key] = need
}
}
}
return -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{0, 1, 1}, {1, 1, 0}, {1, 1, 0}}, 2},
{"TestCase2", [][]int{{0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 1, 0}}, 0},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading