diff --git a/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/README.md b/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/README.md index 29935036..9ece2446 100755 --- a/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/README.md +++ b/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/README.md @@ -1,28 +1,51 @@ # [3341.Find Minimum Time to Reach Last Room I][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 exactly one second. + +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: 6 + +Explanation: + +The minimum time required is 6 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 one second. +``` + +**Example 2:** + ``` +Input: moveTime = [[0,4],[4,4]] + +Output: 6 -## 题意 -> ... +Explanation: -## 题解 +The minimum time required is 6 seconds. -### 思路1 -> ... -Find Minimum Time to Reach Last Room I -```go +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 one second. ``` +**Example 3:** + +``` +Input: moveTime = [[0,1],[1,2]] + +Output: 3 +``` ## 结语 diff --git a/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution.go b/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution.go index d115ccf5..fe61d041 100644 --- a/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution.go +++ b/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution.go @@ -1,5 +1,63 @@ package Solution -func Solution(x bool) bool { +import "container/heap" + +type heap3341 [][3]int + +func (h *heap3341) Len() int { + return len(*h) +} + +func (h *heap3341) Swap(i, j int) { + (*h)[i], (*h)[j] = (*h)[j], (*h)[i] +} + +func (h *heap3341) Less(i, j int) bool { + return (*h)[i][2] < (*h)[j][2] +} + +func (h *heap3341) Push(x any) { + *h = append(*h, x.([3]int)) +} + +func (h *heap3341) Pop() any { + old := *h + l := len(old) + x := old[l-1] + *h = old[:l-1] return x } + +var dirs3341 = [][2]int{ + {0, 1}, {0, -1}, {-1, 0}, {1, 0}, +} + +func Solution(moveTime [][]int) int { + m, n := len(moveTime), len(moveTime[0]) + h := heap3341{{0, 0, 0}} + visited := map[[2]int]struct{}{ + [2]int{0, 0}: {}, + } + for h.Len() > 0 { + top := heap.Pop(&h).([3]int) + if top[0] == m-1 && top[1] == n-1 { + return top[2] + } + for _, dir := range dirs3341 { + 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] + 1 + } else { + cost++ + } + heap.Push(&h, [3]int{nx, ny, cost}) + } + } + } + } + return -1 +} diff --git a/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution_test.go b/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution_test.go index 14ff50eb..2168aab5 100644 --- a/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution_test.go +++ b/leetcode/3301-3400/3341.Find-Minimum-Time-to-Reach-Last-Room-I/Solution_test.go @@ -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}}, 6}, + {"TestCase2", [][]int{{0, 0, 0}, {0, 0, 0}}, 3}, + {"TestCase3", [][]int{{0, 1}, {1, 2}}, 3}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }