Skip to content

Commit 806408a

Browse files
committed
Add solution and test-cases for problem 417
1 parent a01e89c commit 806408a

File tree

4 files changed

+116
-24
lines changed

4 files changed

+116
-24
lines changed
Loading

leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [417.Pacific Atlantic Water Flow][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+
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.
75

8-
**Example 1:**
6+
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)`.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
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.
9+
10+
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.
1411

15-
## 题意
16-
> ...
12+
**Example 1:**
1713

18-
## 题解
14+
![1](./1.jpg)
1915

20-
### 思路1
21-
> ...
22-
Pacific Atlantic Water Flow
23-
```go
2416
```
17+
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]]
18+
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
19+
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
20+
[0,4]: [0,4] -> Pacific Ocean
21+
[0,4] -> Atlantic Ocean
22+
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
23+
[1,3] -> [1,4] -> Atlantic Ocean
24+
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
25+
[1,4] -> Atlantic Ocean
26+
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
27+
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
28+
[3,0]: [3,0] -> Pacific Ocean
29+
[3,0] -> [4,0] -> Atlantic Ocean
30+
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
31+
[3,1] -> [4,1] -> Atlantic Ocean
32+
[4,0]: [4,0] -> Pacific Ocean
33+
[4,0] -> Atlantic Ocean
34+
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
35+
```
36+
37+
**Example 2:**
2538

39+
```
40+
Input: heights = [[1]]
41+
Output: [[0,0]]
42+
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
43+
```
2644

2745
## 结语
2846

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(heights [][]int) [][]int {
4+
m, n := len(heights), len(heights[0])
5+
state := make([][]int, m)
6+
pacific, atlantic := 1, 2
7+
for i := 0; i < m; i++ {
8+
state[i] = make([]int, n)
9+
}
10+
for i := 0; i < n-1; i++ {
11+
state[0][i] = pacific
12+
state[m-1][i+1] = atlantic
13+
}
14+
for i := 0; i < m-1; i++ {
15+
state[i][0] = pacific
16+
state[i+1][n-1] = atlantic
17+
}
18+
19+
state[0][n-1] = 3
20+
state[m-1][0] = 3
21+
var (
22+
leftTop func(int, int, int)
23+
rightBottom func(int, int, int)
24+
)
25+
leftTop = func(x, y, pre int) {
26+
if x < 0 || x >= m || y < 0 || y >= n {
27+
return
28+
}
29+
if pre != -1 && state[x][y]&pacific == pacific {
30+
return
31+
}
32+
if heights[x][y] < pre {
33+
return
34+
}
35+
state[x][y] |= pacific
36+
leftTop(x-1, y, heights[x][y])
37+
leftTop(x+1, y, heights[x][y])
38+
leftTop(x, y+1, heights[x][y])
39+
leftTop(x, y-1, heights[x][y])
40+
}
41+
rightBottom = func(x, y, pre int) {
42+
if x < 0 || x >= m || y < 0 || y >= n {
43+
return
44+
}
45+
if heights[x][y] < pre {
46+
return
47+
}
48+
if pre != -1 && state[x][y]&atlantic == atlantic {
49+
return
50+
}
51+
state[x][y] |= atlantic
52+
rightBottom(x-1, y, heights[x][y])
53+
rightBottom(x+1, y, heights[x][y])
54+
rightBottom(x, y-1, heights[x][y])
55+
rightBottom(x, y+1, heights[x][y])
56+
}
57+
58+
for i := 0; i < n; i++ {
59+
leftTop(0, i, -1)
60+
rightBottom(m-1, i, -1)
61+
}
62+
for i := 0; i < m; i++ {
63+
leftTop(i, 0, -1)
64+
rightBottom(i, n-1, -1)
65+
}
66+
res := make([][]int, 0)
67+
for i := 0; i < m; i++ {
68+
for j := 0; j < n; j++ {
69+
if state[i][j] == 3 {
70+
res = append(res, []int{i, j})
71+
}
72+
}
73+
}
74+
75+
return res
576
}

leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ 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+
{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},
18+
}, [][]int{
19+
{0, 4}, {1, 3}, {1, 4}, {2, 2}, {3, 0}, {3, 1}, {4, 0},
20+
}},
21+
{"TestCase2", [][]int{{1}}, [][]int{{0, 0}}},
1922
}
2023

2124
// 开始测试
@@ -30,10 +33,10 @@ func TestSolution(t *testing.T) {
3033
}
3134
}
3235

33-
// 压力测试
36+
// 压力测试
3437
func BenchmarkSolution(b *testing.B) {
3538
}
3639

37-
// 使用案列
40+
// 使用案列
3841
func ExampleSolution() {
3942
}

0 commit comments

Comments
 (0)