Skip to content

Commit 9fb5af9

Browse files
authored
Merge pull request #899 from 0xff-dev/778
Add solution and test-cases for problem 778
2 parents 82b4bea + 5fea722 commit 9fb5af9

File tree

5 files changed

+77
-23
lines changed

5 files changed

+77
-23
lines changed

leetcode/701-800/0778.Swim-in-Rising-Water/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [778.Swim in Rising Water][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+
You are given an `n x n` integer matrix `grid` where each value `grid[i][j]` represents the elevation at that point `(i, j)`.
5+
6+
The rain starts to fall. At time `t`, the depth of the water everywhere is `t`. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most `t`. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.
7+
8+
Return the least time until you can reach the bottom right square `(n - 1, n - 1)` if you start at the top left square `(0, 0)`.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./swim1-grid.jpeg)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: grid = [[0,2],[1,3]]
16+
Output: 3
17+
Explanation:
18+
At time 0, you are in grid location (0, 0).
19+
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
20+
You cannot reach point (1, 1) until time 3.
21+
When the depth of water is 3, we can swim anywhere inside the grid.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./swim2-grid-1.jpeg)
1927

20-
### 思路1
21-
> ...
22-
Swim in Rising Water
23-
```go
2428
```
25-
29+
Input: grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
30+
Output: 16
31+
Explanation: The final route is shown.
32+
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
33+
```
2634

2735
## 结语
2836

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func dfs778(grid [][]int, visited [][]bool, x, y, t int) bool {
4+
if x < 0 || x >= len(grid) || y < 0 || y >= len(grid) || visited[x][y] || grid[x][y] > t {
5+
return false
6+
}
7+
if x == len(grid)-1 && y == len(grid)-1 {
8+
return true
9+
}
10+
visited[x][y] = true
11+
return dfs778(grid, visited, x-1, y, t) ||
12+
dfs778(grid, visited, x+1, y, t) ||
13+
dfs778(grid, visited, x, y-1, t) ||
14+
dfs778(grid, visited, x, y+1, t)
15+
16+
}
17+
func do778(grid [][]int, x, y, t int) bool {
18+
gridCopy := make([][]int, len(grid))
19+
v := make([][]bool, len(grid))
20+
for i := 0; i < len(grid); i++ {
21+
gridCopy[i] = make([]int, len(grid))
22+
v[i] = make([]bool, len(grid))
23+
copy(gridCopy[i], grid[i])
24+
}
25+
26+
return dfs778(gridCopy, v, x, y, t)
27+
}
28+
func Solution(grid [][]int) int {
29+
30+
// 一般这种dfs或者bfs都不太好确定的题,可以尝试binary search+dfs
31+
// 直接尝试搜索每个答案
32+
ans := -1
33+
l, r := 0, 1<<31-1
34+
for l <= r {
35+
mid := l + (r-l)/2
36+
if do778(grid, 0, 0, mid) {
37+
ans = mid
38+
r = mid - 1
39+
} else {
40+
l = mid + 1
41+
}
42+
}
43+
return ans
544
}

leetcode/701-800/0778.Swim-in-Rising-Water/Solution_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ 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{
17+
{0, 2}, {1, 3},
18+
}, 3},
19+
{"TestCase1", [][]int{
20+
{0, 1, 2, 3, 4},
21+
{24, 23, 22, 21, 5},
22+
{12, 13, 14, 15, 16},
23+
{11, 17, 18, 19, 20},
24+
{10, 9, 8, 7, 6},
25+
}, 16},
1926
}
2027

2128
// 开始测试
@@ -30,10 +37,10 @@ func TestSolution(t *testing.T) {
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}
Loading
Loading

0 commit comments

Comments
 (0)