Skip to content

Add solution and test-cases for problem 417 #1193

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 33 additions & 15 deletions leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
75 changes: 73 additions & 2 deletions leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 10 additions & 7 deletions leetcode/401-500/0417.Pacific-Atlantic-Water-Flow/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}},
}

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

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading