diff --git a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/1.jpg b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/1.jpg new file mode 100644 index 000000000..825101273 Binary files /dev/null and b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/1.jpg differ diff --git a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/README.md b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/README.md index 9d7eb01c3..edb225691 100644 --- a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/README.md +++ b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/README.md @@ -1,28 +1,46 @@ # [417.Pacific Atlantic Water Flow][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 +There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges. -**Example 1:** +The island is partitioned into a grid of square cells. You are given an `m x n` integer matrix `heights` where `heights[r][c]` represents the **height above sea level** of the cell at coordinate `(r, c)`. -``` -Input: a = "11", b = "1" -Output: "100" -``` +The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is **less than or equal to** the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean. + +Return a **2D list** of grid coordinates `result` where `result[i] = [ri, ci]` denotes that rain water can flow from cell `(ri, ci)` to **both** the Pacific and Atlantic oceans. -## 题意 -> ... +**Example 1:** -## 题解 +![1](./1.jpg) -### 思路1 -> ... -Pacific Atlantic Water Flow -```go ``` +Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] +Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] +Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: +[0,4]: [0,4] -> Pacific Ocean + [0,4] -> Atlantic Ocean +[1,3]: [1,3] -> [0,3] -> Pacific Ocean + [1,3] -> [1,4] -> Atlantic Ocean +[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean + [1,4] -> Atlantic Ocean +[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean + [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean +[3,0]: [3,0] -> Pacific Ocean + [3,0] -> [4,0] -> Atlantic Ocean +[3,1]: [3,1] -> [3,0] -> Pacific Ocean + [3,1] -> [4,1] -> Atlantic Ocean +[4,0]: [4,0] -> Pacific Ocean + [4,0] -> Atlantic Ocean +Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans. +``` + +**Example 2:** +``` +Input: heights = [[1]] +Output: [[0,0]] +Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans. +``` ## 结语 diff --git a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution.go b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution.go index d115ccf5e..49eeaa936 100644 --- a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution.go +++ b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution.go @@ -1,5 +1,76 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(heights [][]int) [][]int { + m, n := len(heights), len(heights[0]) + state := make([][]int, m) + pacific, atlantic := 1, 2 + for i := 0; i < m; i++ { + state[i] = make([]int, n) + } + for i := 0; i < n-1; i++ { + state[0][i] = pacific + state[m-1][i+1] = atlantic + } + for i := 0; i < m-1; i++ { + state[i][0] = pacific + state[i+1][n-1] = atlantic + } + + state[0][n-1] = 3 + state[m-1][0] = 3 + var ( + leftTop func(int, int, int) + rightBottom func(int, int, int) + ) + leftTop = func(x, y, pre int) { + if x < 0 || x >= m || y < 0 || y >= n { + return + } + if pre != -1 && state[x][y]&pacific == pacific { + return + } + if heights[x][y] < pre { + return + } + state[x][y] |= pacific + leftTop(x-1, y, heights[x][y]) + leftTop(x+1, y, heights[x][y]) + leftTop(x, y+1, heights[x][y]) + leftTop(x, y-1, heights[x][y]) + } + rightBottom = func(x, y, pre int) { + if x < 0 || x >= m || y < 0 || y >= n { + return + } + if heights[x][y] < pre { + return + } + if pre != -1 && state[x][y]&atlantic == atlantic { + return + } + state[x][y] |= atlantic + rightBottom(x-1, y, heights[x][y]) + rightBottom(x+1, y, heights[x][y]) + rightBottom(x, y-1, heights[x][y]) + rightBottom(x, y+1, heights[x][y]) + } + + for i := 0; i < n; i++ { + leftTop(0, i, -1) + rightBottom(m-1, i, -1) + } + for i := 0; i < m; i++ { + leftTop(i, 0, -1) + rightBottom(i, n-1, -1) + } + res := make([][]int, 0) + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if state[i][j] == 3 { + res = append(res, []int{i, j}) + } + } + } + + return res } diff --git a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution_test.go b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution_test.go index 14ff50eb4..ba5d796c6 100644 --- a/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution_test.go +++ b/leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution_test.go @@ -10,12 +10,15 @@ 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{ + {1, 2, 2, 3, 5}, {3, 2, 3, 4, 4}, {2, 4, 5, 3, 1}, {6, 7, 1, 4, 5}, {5, 1, 1, 2, 4}, + }, [][]int{ + {0, 4}, {1, 3}, {1, 4}, {2, 2}, {3, 0}, {3, 1}, {4, 0}, + }}, + {"TestCase2", [][]int{{1}}, [][]int{{0, 0}}}, } // 开始测试 @@ -30,10 +33,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }