Skip to content
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

Add solution and test-cases for problem 778 #899

Merged
merged 1 commit into from
Jun 14, 2024
Merged
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
36 changes: 22 additions & 14 deletions leetcode/701-800/0778.Swim-in-Rising-Water/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [778.Swim in Rising Water][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
You are given an `n x n` integer matrix `grid` where each value `grid[i][j]` represents the elevation at that point `(i, j)`.

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.

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)`.

**Example 1:**
**Example 1:**

![1](./swim1-grid.jpeg)

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

## 题意
> ...
**Example 2:**

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

### 思路1
> ...
Swim in Rising Water
```go
```

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]]
Output: 16
Explanation: The final route is shown.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
```

## 结语

Expand Down
43 changes: 41 additions & 2 deletions leetcode/701-800/0778.Swim-in-Rising-Water/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package Solution

func Solution(x bool) bool {
return x
func dfs778(grid [][]int, visited [][]bool, x, y, t int) bool {
if x < 0 || x >= len(grid) || y < 0 || y >= len(grid) || visited[x][y] || grid[x][y] > t {
return false
}
if x == len(grid)-1 && y == len(grid)-1 {
return true
}
visited[x][y] = true
return dfs778(grid, visited, x-1, y, t) ||
dfs778(grid, visited, x+1, y, t) ||
dfs778(grid, visited, x, y-1, t) ||
dfs778(grid, visited, x, y+1, t)

}
func do778(grid [][]int, x, y, t int) bool {
gridCopy := make([][]int, len(grid))
v := make([][]bool, len(grid))
for i := 0; i < len(grid); i++ {
gridCopy[i] = make([]int, len(grid))
v[i] = make([]bool, len(grid))
copy(gridCopy[i], grid[i])
}

return dfs778(gridCopy, v, x, y, t)
}
func Solution(grid [][]int) int {

// 一般这种dfs或者bfs都不太好确定的题,可以尝试binary search+dfs
// 直接尝试搜索每个答案
ans := -1
l, r := 0, 1<<31-1
for l <= r {
mid := l + (r-l)/2
if do778(grid, 0, 0, mid) {
ans = mid
r = mid - 1
} else {
l = mid + 1
}
}
return ans
}
21 changes: 14 additions & 7 deletions leetcode/701-800/0778.Swim-in-Rising-Water/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ 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, 2}, {1, 3},
}, 3},
{"TestCase1", [][]int{
{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},
}, 16},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
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.
Loading