Skip to content

Commit d22d762

Browse files
committed
Add solution and test-cases for problem 2577
1 parent dbd4dda commit d22d762

File tree

5 files changed

+115
-8
lines changed

5 files changed

+115
-8
lines changed
6.83 KB
Loading
4.71 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [2577.Minimum Time to Visit a Cell In a Grid][title]
2+
3+
## Description
4+
You are given a `m x n` matrix `grid` consisting of **non-negative** integers where `grid[row][col]` represents the **minimum** time required to be able to visit the cell `(row, col)`, which means you can visit the cell `(row, col)` only when the time you visit it is greater than or equal to `grid[row][col]`.
5+
6+
You are standing in the **top-left** cell of the matrix in the `0th` second, and you must move to **any** adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.
7+
8+
Return the **minimum** time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the bottom-right cell, then return `-1`.
9+
10+
**Example 1:**
11+
12+
![1](./1.png)
13+
14+
```
15+
Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]
16+
Output: 7
17+
Explanation: One of the paths that we can take is the following:
18+
- at t = 0, we are on the cell (0,0).
19+
- at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.
20+
- at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.
21+
- at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.
22+
- at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.
23+
- at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.
24+
- at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.
25+
- at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7.
26+
The final time is 7. It can be shown that it is the minimum time possible.
27+
```
28+
29+
**Example 2:**
30+
31+
![2](./2.png)
32+
33+
```
34+
Input: grid = [[0,2,4],[3,2,1],[1,0,4]]
35+
Output: -1
36+
Explanation: There is no path from the top left to the bottom-right cell.
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
var dirs2577 = [][2]int{
6+
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
7+
}
8+
9+
type heap2577 [][3]int
10+
11+
func (h *heap2577) Len() int {
12+
return len(*h)
13+
}
14+
15+
func (h *heap2577) Swap(i, j int) {
16+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
17+
}
18+
19+
func (h *heap2577) Less(i, j int) bool {
20+
return (*h)[i][2] < (*h)[j][2]
21+
}
22+
23+
func (h *heap2577) Push(x any) {
24+
*h = append(*h, x.([3]int))
25+
}
26+
27+
func (h *heap2577) Pop() any {
28+
old := *h
29+
l := len(old)
30+
x := old[l-1]
31+
*h = old[:l-1]
432
return x
533
}
34+
35+
func Solution(grid [][]int) int {
36+
if grid[0][1] > 1 && grid[1][0] > 1 {
37+
// 根本走不了
38+
return -1
39+
}
40+
m, n := len(grid), len(grid[0])
41+
visited := make(map[[2]int]int)
42+
visited[[2]int{0, 0}] = 0
43+
q := heap2577{{0, 0, 0}}
44+
45+
for q.Len() > 0 {
46+
cur := heap.Pop(&q).([3]int)
47+
if cur[0] == m-1 && cur[1] == n-1 {
48+
return cur[2]
49+
}
50+
for _, dir := range dirs2577 {
51+
nx, ny := cur[0]+dir[0], cur[1]+dir[1]
52+
if nx >= 0 && nx < m && ny >= 0 && ny < n {
53+
cost := cur[2] + 1 // 耗时比目前小,直接+1过去
54+
if diff := grid[nx][ny] - cur[2]; diff >= 1 {
55+
cost = grid[nx][ny]
56+
if diff&1 == 0 {
57+
cost++
58+
}
59+
}
60+
if v, ok := visited[[2]int{nx, ny}]; !ok || v > cost {
61+
heap.Push(&q, [3]int{nx, ny, cost})
62+
visited[[2]int{nx, ny}] = cost
63+
}
64+
}
65+
}
66+
}
67+
68+
return -1
69+
}

leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/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, 3, 2}, {5, 1, 2, 5}, {4, 3, 8, 6}}, 7},
17+
{"TestCase2", [][]int{{0, 2, 4}, {3, 2, 1}, {1, 0, 4}}, -1},
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)