diff --git a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/1.png b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/1.png new file mode 100644 index 000000000..df85556dc Binary files /dev/null and b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/1.png differ diff --git a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/2.png b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/2.png new file mode 100644 index 000000000..598fc2ce7 Binary files /dev/null and b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/2.png differ diff --git a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/README.md b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/README.md new file mode 100644 index 000000000..e96c145ca --- /dev/null +++ b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/README.md @@ -0,0 +1,44 @@ +# [2577.Minimum Time to Visit a Cell In a Grid][title] + +## Description +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]`. + +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. + +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`. + +**Example 1:** + +![1](./1.png) + +``` +Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]] +Output: 7 +Explanation: One of the paths that we can take is the following: +- at t = 0, we are on the cell (0,0). +- at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1. +- at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2. +- at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3. +- at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4. +- at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5. +- at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6. +- at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7. +The final time is 7. It can be shown that it is the minimum time possible. +``` + +**Example 2:** + +![2](./2.png) + +``` +Input: grid = [[0,2,4],[3,2,1],[1,0,4]] +Output: -1 +Explanation: There is no path from the top left to the bottom-right cell. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution.go b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution.go index d115ccf5e..954430a65 100755 --- a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution.go +++ b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution.go @@ -1,5 +1,69 @@ package Solution -func Solution(x bool) bool { +import "container/heap" + +var dirs2577 = [][2]int{ + {0, 1}, {1, 0}, {0, -1}, {-1, 0}, +} + +type heap2577 [][3]int + +func (h *heap2577) Len() int { + return len(*h) +} + +func (h *heap2577) Swap(i, j int) { + (*h)[i], (*h)[j] = (*h)[j], (*h)[i] +} + +func (h *heap2577) Less(i, j int) bool { + return (*h)[i][2] < (*h)[j][2] +} + +func (h *heap2577) Push(x any) { + *h = append(*h, x.([3]int)) +} + +func (h *heap2577) Pop() any { + old := *h + l := len(old) + x := old[l-1] + *h = old[:l-1] return x } + +func Solution(grid [][]int) int { + if grid[0][1] > 1 && grid[1][0] > 1 { + // 根本走不了 + return -1 + } + m, n := len(grid), len(grid[0]) + visited := make(map[[2]int]int) + visited[[2]int{0, 0}] = 0 + q := heap2577{{0, 0, 0}} + + for q.Len() > 0 { + cur := heap.Pop(&q).([3]int) + if cur[0] == m-1 && cur[1] == n-1 { + return cur[2] + } + for _, dir := range dirs2577 { + nx, ny := cur[0]+dir[0], cur[1]+dir[1] + if nx >= 0 && nx < m && ny >= 0 && ny < n { + cost := cur[2] + 1 // 耗时比目前小,直接+1过去 + if diff := grid[nx][ny] - cur[2]; diff >= 1 { + cost = grid[nx][ny] + if diff&1 == 0 { + cost++ + } + } + if v, ok := visited[[2]int{nx, ny}]; !ok || v > cost { + heap.Push(&q, [3]int{nx, ny, cost}) + visited[[2]int{nx, ny}] = cost + } + } + } + } + + return -1 +} diff --git a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution_test.go b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution_test.go index 14ff50eb4..2917d5fbc 100755 --- a/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution_test.go +++ b/leetcode/2501-2600/2577.Minimum-Time-to-Visit-a-Cell-In-a-Grid/Solution_test.go @@ -10,12 +10,11 @@ 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, 1, 3, 2}, {5, 1, 2, 5}, {4, 3, 8, 6}}, 7}, + {"TestCase2", [][]int{{0, 2, 4}, {3, 2, 1}, {1, 0, 4}}, -1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }