diff --git a/leetcode/701-800/0778.Swim-in-Rising-Water/README.md b/leetcode/701-800/0778.Swim-in-Rising-Water/README.md index e39c46e06..c1e644e74 100644 --- a/leetcode/701-800/0778.Swim-in-Rising-Water/README.md +++ b/leetcode/701-800/0778.Swim-in-Rising-Water/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/701-800/0778.Swim-in-Rising-Water/Solution.go b/leetcode/701-800/0778.Swim-in-Rising-Water/Solution.go index d115ccf5e..7cc68e6ec 100644 --- a/leetcode/701-800/0778.Swim-in-Rising-Water/Solution.go +++ b/leetcode/701-800/0778.Swim-in-Rising-Water/Solution.go @@ -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 } diff --git a/leetcode/701-800/0778.Swim-in-Rising-Water/Solution_test.go b/leetcode/701-800/0778.Swim-in-Rising-Water/Solution_test.go index 14ff50eb4..dd43699ef 100644 --- a/leetcode/701-800/0778.Swim-in-Rising-Water/Solution_test.go +++ b/leetcode/701-800/0778.Swim-in-Rising-Water/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +37,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/701-800/0778.Swim-in-Rising-Water/swim1-grid.jpeg b/leetcode/701-800/0778.Swim-in-Rising-Water/swim1-grid.jpeg new file mode 100644 index 000000000..062429fc0 Binary files /dev/null and b/leetcode/701-800/0778.Swim-in-Rising-Water/swim1-grid.jpeg differ diff --git a/leetcode/701-800/0778.Swim-in-Rising-Water/swim2-grid-1.jpeg b/leetcode/701-800/0778.Swim-in-Rising-Water/swim2-grid-1.jpeg new file mode 100644 index 000000000..c9127d764 Binary files /dev/null and b/leetcode/701-800/0778.Swim-in-Rising-Water/swim2-grid-1.jpeg differ