Skip to content

Commit

Permalink
Merge pull request #1047 from 0xff-dev/2577
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2577
  • Loading branch information
6boris authored Dec 24, 2024
2 parents ce3fb65 + d22d762 commit 14aa847
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

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

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

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

0 comments on commit 14aa847

Please sign in to comment.