Skip to content

Add solution and test-cases for problem 3342 #1184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
# [3342.Find Minimum Time to Reach Last Room II][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
There is a dungeon with `n x m` rooms arranged as a grid.

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.

Return the **minimum** time to reach the room `(n - 1, m - 1)`.

Two rooms are **adjacent** if they share a common wall, either horizontally or vertically.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: moveTime = [[0,4],[4,4]]

Output: 7

Explanation:

The minimum time required is 7 seconds.

At time t == 4, move from room (0, 0) to room (1, 0) in one second.
At time t == 5, move from room (1, 0) to room (1, 1) in two seconds.
```

**Example 2:**

```
Input: moveTime = [[0,0,0,0],[0,0,0,0]]

Output: 6

## 题意
> ...
Explanation:

## 题解
The minimum time required is 6 seconds.

### 思路1
> ...
Find Minimum Time to Reach Last Room II
```go
At time t == 0, move from room (0, 0) to room (1, 0) in one second.
At time t == 1, move from room (1, 0) to room (1, 1) in two seconds.
At time t == 3, move from room (1, 1) to room (1, 2) in one second.
At time t == 4, move from room (1, 2) to room (1, 3) in two seconds.
```

**Example 3:**

```
Input: moveTime = [[0,1],[1,2]]

Output: 4
```

## 结语

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

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

type heap3342 [][4]int

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

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

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

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

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

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

func Solution(moveTime [][]int) int {
m, n := len(moveTime), len(moveTime[0])
h := heap3342{{0, 0, 0, 1}}
visited := map[[2]int]struct{}{
[2]int{0, 0}: {},
}
for h.Len() > 0 {
top := heap.Pop(&h).([4]int)
if top[0] == m-1 && top[1] == n-1 {
return top[2]
}
for _, dir := range dirs3342 {
nx, ny := top[0]+dir[0], top[1]+dir[1]
if nx >= 0 && nx < m && ny >= 0 && ny < n {
if _, ok := visited[[2]int{nx, ny}]; !ok {
visited[[2]int{nx, ny}] = struct{}{}
cost := moveTime[nx][ny]
if cost <= top[2] {
cost = top[2] + top[3]
} else {
cost += top[3]
}
heap.Push(&h, [4]int{nx, ny, cost, 3 - top[3]})
}
}
}
}
return -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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, 4}, {4, 4}}, 7},
{"TestCase2", [][]int{{0, 0, 0, 0}, {0, 0, 0, 0}}, 6},
{"TestCase3", [][]int{{0, 1}, {1, 2}}, 4},
}

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

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

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