Skip to content

Commit d98de7e

Browse files
authored
Merge pull request #1098 from 0xff-dev/1368
Add solution and test-cases for problem 1368
2 parents c044893 + 0337aae commit d98de7e

File tree

6 files changed

+136
-8
lines changed

6 files changed

+136
-8
lines changed
15.3 KB
Loading
4.18 KB
Loading
2.44 KB
Loading
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [1368.Minimum Cost to Make at Least One Valid Path in a Grid][title]
2+
3+
## Description
4+
Given an `m x n` grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of `grid[i][j]` can be:
5+
6+
- `1` which means go to the cell to the right. (i.e go from `grid[i][j]` to `grid[i][j + 1]`)
7+
- `2` which means go to the cell to the left. (i.e go from `grid[i][j]` to `grid[i][j - 1]`)
8+
- `3` which means go to the lower cell. (i.e go from `grid[i][j]` to `grid[i + 1][j]`)
9+
- `4` which means go to the upper cell. (i.e go from `grid[i][j]` to `grid[i - 1][j]`)
10+
11+
Notice that there could be some signs on the cells of the grid that point outside the grid.
12+
13+
You will initially start at the upper left cell (`0, 0`). A valid path in the grid is a path that starts from the upper left cell (`0, 0`) and ends at the bottom-right cell (`m - 1, n - 1`) following the signs on the grid. The valid path does not have to be the shortest.
14+
15+
You can modify the sign on a cell with `cost = 1`. You can modify the sign on a cell **one time only**.
16+
17+
Return the minimum cost to make the grid have at least one valid path.
18+
19+
**Example 1:**
20+
21+
![1](./1.png)
22+
23+
```
24+
Input: grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
25+
Output: 3
26+
Explanation: You will start at point (0, 0).
27+
The path to (3, 3) is as follows. (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) change the arrow to down with cost = 1 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) change the arrow to down with cost = 1 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) change the arrow to down with cost = 1 --> (3, 3)
28+
The total cost = 3.
29+
```
30+
31+
**Example 2:**
32+
33+
![2](./2.png)
34+
35+
```
36+
Input: grid = [[1,1,3],[3,2,2],[1,1,4]]
37+
Output: 0
38+
Explanation: You can follow the path from (0, 0) to (2, 2).
39+
```
40+
41+
**Example 3:**
42+
43+
![3](./3.png)
44+
45+
```
46+
Input: grid = [[1,2],[4,3]]
47+
Output: 1
48+
```
49+
50+
## 结语
51+
52+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
53+
54+
[title]: https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid
55+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
// x, y, cost, dir
6+
type heap1368 [][4]int
7+
8+
func (h *heap1368) Len() int {
9+
return len(*h)
10+
}
11+
func (h *heap1368) Swap(i, j int) {
12+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
13+
}
14+
15+
func (h *heap1368) Less(i, j int) bool {
16+
return (*h)[i][2] < (*h)[j][2]
17+
}
18+
func (h *heap1368) Push(x any) {
19+
*h = append(*h, x.([4]int))
20+
}
21+
22+
func (h *heap1368) Pop() any {
23+
old := *h
24+
l := len(old)
25+
x := old[l-1]
26+
*h = old[:l-1]
427
return x
528
}
29+
30+
var dir1368 = [4][2]int{
31+
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
32+
}
33+
34+
func Solution(grid [][]int) int {
35+
m, n := len(grid), len(grid[0])
36+
if m == 1 && n == 1 {
37+
return 0
38+
}
39+
inf := m*n + 1
40+
dp := make([][]int, m)
41+
for i := range m {
42+
dp[i] = make([]int, n)
43+
for j := range n {
44+
dp[i][j] = inf
45+
}
46+
}
47+
// 就是优先队列
48+
dp[0][0] = 0
49+
h := &heap1368{}
50+
if grid[0][0]&1 == 0 {
51+
dp[0][0] = 1
52+
heap.Push(h, [4]int{0, 0, 1, 1})
53+
heap.Push(h, [4]int{0, 0, 1, 3})
54+
} else {
55+
heap.Push(h, [4]int{0, 0, 0, grid[0][0]})
56+
}
57+
for h.Len() > 0 {
58+
cur := heap.Pop(h).([4]int)
59+
if cur[0] == m-1 && cur[1] == n-1 {
60+
return cur[2]
61+
}
62+
for i, d := range dir1368 {
63+
nx, ny := cur[0]+d[0], cur[1]+d[1]
64+
if nx < m && nx >= 0 && ny < n && ny >= 0 {
65+
cost := cur[2]
66+
if i != cur[3]-1 {
67+
cost++
68+
}
69+
if dp[nx][ny] > cost {
70+
dp[nx][ny] = cost
71+
heap.Push(h, [4]int{nx, ny, cost, grid[nx][ny]})
72+
}
73+
}
74+
}
75+
}
76+
77+
return -1
78+
}

leetcode/1301-1400/1368.Minimum-Cost-to-Make-at-Least-One-Valid-Path-in-a-Grid/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{{1, 1, 1, 1}, {2, 2, 2, 2}, {1, 1, 1, 1}, {2, 2, 2, 2}}, 3},
17+
{"TestCase2", [][]int{{1, 1, 3}, {3, 2, 2}, {1, 1, 4}}, 0},
18+
{"TestCase3", [][]int{{1, 2}, {4, 3}}, 1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)