Skip to content

Commit c386dc2

Browse files
authored
Merge pull request #1184 from 0xff-dev/3342
Add solution and test-cases for problem 3342
2 parents 732fbb6 + 108da1c commit c386dc2

File tree

3 files changed

+103
-20
lines changed

3 files changed

+103
-20
lines changed

leetcode/3301-3400/3342.Find-Minimum-Time-to-Reach-Last-Room-II/README.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
# [3342.Find Minimum Time to Reach Last Room II][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+
There is a dungeon with `n x m` rooms arranged as a grid.
5+
6+
You are given a 2D array `moveTime` of size `n x m`, where `moveTime[i][j]` represents the **minimum** time in seconds when you can **start moving** to that room. You start from the room `(0, 0)` at time `t = 0` and can move to an **adjacent** room. Moving between **adjacent** rooms takes one second for one move and two seconds for the next, **alternating** between the two.
7+
8+
Return the **minimum** time to reach the room `(n - 1, m - 1)`.
9+
10+
Two rooms are **adjacent** if they share a common wall, either horizontally or vertically.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: moveTime = [[0,4],[4,4]]
16+
17+
Output: 7
18+
19+
Explanation:
20+
21+
The minimum time required is 7 seconds.
22+
23+
At time t == 4, move from room (0, 0) to room (1, 0) in one second.
24+
At time t == 5, move from room (1, 0) to room (1, 1) in two seconds.
25+
```
26+
27+
**Example 2:**
28+
1329
```
30+
Input: moveTime = [[0,0,0,0],[0,0,0,0]]
31+
32+
Output: 6
1433
15-
## 题意
16-
> ...
34+
Explanation:
1735
18-
## 题解
36+
The minimum time required is 6 seconds.
1937
20-
### 思路1
21-
> ...
22-
Find Minimum Time to Reach Last Room II
23-
```go
38+
At time t == 0, move from room (0, 0) to room (1, 0) in one second.
39+
At time t == 1, move from room (1, 0) to room (1, 1) in two seconds.
40+
At time t == 3, move from room (1, 1) to room (1, 2) in one second.
41+
At time t == 4, move from room (1, 2) to room (1, 3) in two seconds.
2442
```
2543

44+
**Example 3:**
45+
46+
```
47+
Input: moveTime = [[0,1],[1,2]]
48+
49+
Output: 4
50+
```
2651

2752
## 结语
2853

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

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type heap3342 [][4]int
6+
7+
func (h *heap3342) Len() int {
8+
return len(*h)
9+
}
10+
11+
func (h *heap3342) Swap(i, j int) {
12+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
13+
}
14+
15+
func (h *heap3342) Less(i, j int) bool {
16+
return (*h)[i][2] < (*h)[j][2]
17+
}
18+
19+
func (h *heap3342) Push(x any) {
20+
*h = append(*h, x.([4]int))
21+
}
22+
23+
func (h *heap3342) 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 dirs3342 = [][2]int{
32+
{0, 1}, {0, -1}, {-1, 0}, {1, 0},
33+
}
34+
35+
func Solution(moveTime [][]int) int {
36+
m, n := len(moveTime), len(moveTime[0])
37+
h := heap3342{{0, 0, 0, 1}}
38+
visited := map[[2]int]struct{}{
39+
[2]int{0, 0}: {},
40+
}
41+
for h.Len() > 0 {
42+
top := heap.Pop(&h).([4]int)
43+
if top[0] == m-1 && top[1] == n-1 {
44+
return top[2]
45+
}
46+
for _, dir := range dirs3342 {
47+
nx, ny := top[0]+dir[0], top[1]+dir[1]
48+
if nx >= 0 && nx < m && ny >= 0 && ny < n {
49+
if _, ok := visited[[2]int{nx, ny}]; !ok {
50+
visited[[2]int{nx, ny}] = struct{}{}
51+
cost := moveTime[nx][ny]
52+
if cost <= top[2] {
53+
cost = top[2] + top[3]
54+
} else {
55+
cost += top[3]
56+
}
57+
heap.Push(&h, [4]int{nx, ny, cost, 3 - top[3]})
58+
}
59+
}
60+
}
61+
}
62+
return -1
63+
}

leetcode/3301-3400/3342.Find-Minimum-Time-to-Reach-Last-Room-II/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{{0, 4}, {4, 4}}, 7},
17+
{"TestCase2", [][]int{{0, 0, 0, 0}, {0, 0, 0, 0}}, 6},
18+
{"TestCase3", [][]int{{0, 1}, {1, 2}}, 4},
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)