Skip to content

Commit e53ccdd

Browse files
committedFeb 5, 2022
feat: add solutions to lc problem: No.0417
No.0417.Pacific Atlantic Water Flow
1 parent a4dd957 commit e53ccdd

File tree

6 files changed

+490
-5
lines changed

6 files changed

+490
-5
lines changed
 

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
## 算法全解
2727

2828
- [LeetCode](/solution/README.md)
29-
- [LeetCode 《剑指 Offer(第 2 版)](/lcof/README.md)
30-
- [LeetCode 《剑指 Offer(专项突击版)](/lcof2/README.md)
31-
- [LeetCode 《程序员面试金典(第 6 版)](/lcci/README.md)
29+
- [剑指 Offer(第 2 版)](/lcof/README.md)
30+
- [剑指 Offer(专项突击版)](/lcof2/README.md)
31+
- [程序员面试金典(第 6 版)](/lcci/README.md)
3232

3333
## 基础算法通关
3434

‎solution/0400-0499/0417.Pacific Atlantic Water Flow/README.md

+165-1
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,186 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
反向寻找,从海洋开始逆流,只要比该陆地高的地方就能逆流。最后是寻找两个海洋的水流都能经过的陆地即为结果。
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
<!-- 这里可写当前语言的特殊实现逻辑 -->
5759

5860
```python
59-
61+
class Solution:
62+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
63+
def bfs(q, vis):
64+
while q:
65+
for _ in range(len(q), 0, -1):
66+
i, j = q.popleft()
67+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
68+
x, y = i + a, j + b
69+
if 0 <= x < m and 0 <= y < n and (x, y) not in vis and heights[x][y] >= heights[i][j]:
70+
vis.add((x, y))
71+
q.append((x, y))
72+
73+
m, n = len(heights), len(heights[0])
74+
vis1, vis2 = set(), set()
75+
q1 = deque()
76+
q2 = deque()
77+
for i in range(m):
78+
for j in range(n):
79+
if i == 0 or j == 0:
80+
vis1.add((i, j))
81+
q1.append((i, j))
82+
if i == m - 1 or j == n - 1:
83+
vis2.add((i, j))
84+
q2.append((i, j))
85+
bfs(q1, vis1)
86+
bfs(q2, vis2)
87+
ans = []
88+
for i in range(m):
89+
for j in range(n):
90+
if (i, j) in vis1 and (i, j) in vis2:
91+
ans.append((i, j))
92+
return ans
6093
```
6194

6295
### **Java**
6396

6497
<!-- 这里可写当前语言的特殊实现逻辑 -->
6598

6699
```java
100+
class Solution {
101+
private int[][] heights;
102+
private int m;
103+
private int n;
104+
105+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
106+
m = heights.length;
107+
n = heights[0].length;
108+
this.heights = heights;
109+
Deque<int[]> q1 = new LinkedList<>();
110+
Deque<int[]> q2 = new LinkedList<>();
111+
Set<Integer> vis1 = new HashSet<>();
112+
Set<Integer> vis2 = new HashSet<>();
113+
for (int i = 0; i < m; ++i) {
114+
for (int j = 0; j < n; ++j) {
115+
if (i == 0 || j == 0) {
116+
vis1.add(i * n + j);
117+
q1.offer(new int[]{i, j});
118+
}
119+
if (i == m - 1 || j == n - 1) {
120+
vis2.add(i * n + j);
121+
q2.offer(new int[]{i, j});
122+
}
123+
}
124+
}
125+
bfs(q1, vis1);
126+
bfs(q2, vis2);
127+
List<List<Integer>> ans = new ArrayList<>();
128+
for (int i = 0; i < m; ++i) {
129+
for (int j = 0; j < n; ++j) {
130+
int x = i * n + j;
131+
if (vis1.contains(x) && vis2.contains(x)) {
132+
ans.add(Arrays.asList(i, j));
133+
}
134+
}
135+
}
136+
return ans;
137+
}
138+
139+
private void bfs(Deque<int[]> q, Set<Integer> vis) {
140+
int[] dirs = {-1, 0, 1, 0, -1};
141+
while (!q.isEmpty()) {
142+
for (int k = q.size(); k > 0; --k) {
143+
int[] p = q.poll();
144+
for (int i = 0; i < 4; ++i) {
145+
int x = p[0] + dirs[i];
146+
int y = p[1] + dirs[i + 1];
147+
if (x >= 0 && x < m && y >= 0 && y < n && !vis.contains(x * n + y) && heights[x][y] >= heights[p[0]][p[1]]) {
148+
vis.add(x * n + y);
149+
q.offer(new int[]{x, y});
150+
}
151+
}
152+
}
153+
}
154+
}
155+
}
156+
```
67157

158+
### **C++**
159+
160+
```cpp
161+
typedef pair<int, int> pii;
162+
163+
class Solution {
164+
public:
165+
vector<vector<int>> heights;
166+
int m;
167+
int n;
168+
169+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
170+
m = heights.size();
171+
n = heights[0].size();
172+
this->heights = heights;
173+
queue<pii> q1;
174+
queue<pii> q2;
175+
unordered_set<int> vis1;
176+
unordered_set<int> vis2;
177+
for (int i = 0; i < m; ++i)
178+
{
179+
for (int j = 0; j < n; ++j)
180+
{
181+
if (i == 0 || j == 0)
182+
{
183+
vis1.insert(i * n + j);
184+
q1.emplace(i, j);
185+
}
186+
if (i == m - 1 || j == n - 1)
187+
{
188+
vis2.insert(i * n + j);
189+
q2.emplace(i, j);
190+
}
191+
}
192+
}
193+
bfs(q1, vis1);
194+
bfs(q2, vis2);
195+
vector<vector<int>> ans;
196+
for (int i = 0; i < m; ++i)
197+
{
198+
for (int j = 0; j < n; ++j)
199+
{
200+
int x = i * n + j;
201+
if (vis1.count(x) && vis2.count(x))
202+
{
203+
ans.push_back({i, j});
204+
}
205+
}
206+
}
207+
return ans;
208+
}
209+
210+
void bfs(queue<pii>& q, unordered_set<int>& vis) {
211+
vector<int> dirs = {-1, 0, 1, 0, -1};
212+
while (!q.empty())
213+
{
214+
for (int k = q.size(); k > 0; --k)
215+
{
216+
auto p = q.front();
217+
q.pop();
218+
for (int i = 0; i < 4; ++i)
219+
{
220+
int x = p.first + dirs[i];
221+
int y = p.second + dirs[i + 1];
222+
if (x >= 0 && x < m && y >= 0 && y < n && !vis.count(x * n + y) && heights[x][y] >= heights[p.first][p.second])
223+
{
224+
vis.insert(x * n + y);
225+
q.emplace(x, y);
226+
}
227+
}
228+
}
229+
}
230+
}
231+
};
68232
```
69233

70234
### **...**

‎solution/0400-0499/0417.Pacific Atlantic Water Flow/README_EN.md

+163-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,175 @@
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
47+
def bfs(q, vis):
48+
while q:
49+
for _ in range(len(q), 0, -1):
50+
i, j = q.popleft()
51+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
52+
x, y = i + a, j + b
53+
if 0 <= x < m and 0 <= y < n and (x, y) not in vis and heights[x][y] >= heights[i][j]:
54+
vis.add((x, y))
55+
q.append((x, y))
56+
57+
m, n = len(heights), len(heights[0])
58+
vis1, vis2 = set(), set()
59+
q1 = deque()
60+
q2 = deque()
61+
for i in range(m):
62+
for j in range(n):
63+
if i == 0 or j == 0:
64+
vis1.add((i, j))
65+
q1.append((i, j))
66+
if i == m - 1 or j == n - 1:
67+
vis2.add((i, j))
68+
q2.append((i, j))
69+
bfs(q1, vis1)
70+
bfs(q2, vis2)
71+
ans = []
72+
for i in range(m):
73+
for j in range(n):
74+
if (i, j) in vis1 and (i, j) in vis2:
75+
ans.append((i, j))
76+
return ans
4677
```
4778

4879
### **Java**
4980

5081
```java
82+
class Solution {
83+
private int[][] heights;
84+
private int m;
85+
private int n;
86+
87+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
88+
m = heights.length;
89+
n = heights[0].length;
90+
this.heights = heights;
91+
Deque<int[]> q1 = new LinkedList<>();
92+
Deque<int[]> q2 = new LinkedList<>();
93+
Set<Integer> vis1 = new HashSet<>();
94+
Set<Integer> vis2 = new HashSet<>();
95+
for (int i = 0; i < m; ++i) {
96+
for (int j = 0; j < n; ++j) {
97+
if (i == 0 || j == 0) {
98+
vis1.add(i * n + j);
99+
q1.offer(new int[]{i, j});
100+
}
101+
if (i == m - 1 || j == n - 1) {
102+
vis2.add(i * n + j);
103+
q2.offer(new int[]{i, j});
104+
}
105+
}
106+
}
107+
bfs(q1, vis1);
108+
bfs(q2, vis2);
109+
List<List<Integer>> ans = new ArrayList<>();
110+
for (int i = 0; i < m; ++i) {
111+
for (int j = 0; j < n; ++j) {
112+
int x = i * n + j;
113+
if (vis1.contains(x) && vis2.contains(x)) {
114+
ans.add(Arrays.asList(i, j));
115+
}
116+
}
117+
}
118+
return ans;
119+
}
120+
121+
private void bfs(Deque<int[]> q, Set<Integer> vis) {
122+
int[] dirs = {-1, 0, 1, 0, -1};
123+
while (!q.isEmpty()) {
124+
for (int k = q.size(); k > 0; --k) {
125+
int[] p = q.poll();
126+
for (int i = 0; i < 4; ++i) {
127+
int x = p[0] + dirs[i];
128+
int y = p[1] + dirs[i + 1];
129+
if (x >= 0 && x < m && y >= 0 && y < n && !vis.contains(x * n + y) && heights[x][y] >= heights[p[0]][p[1]]) {
130+
vis.add(x * n + y);
131+
q.offer(new int[]{x, y});
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
```
51139

140+
### **C++**
141+
142+
```cpp
143+
typedef pair<int, int> pii;
144+
145+
class Solution {
146+
public:
147+
vector<vector<int>> heights;
148+
int m;
149+
int n;
150+
151+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
152+
m = heights.size();
153+
n = heights[0].size();
154+
this->heights = heights;
155+
queue<pii> q1;
156+
queue<pii> q2;
157+
unordered_set<int> vis1;
158+
unordered_set<int> vis2;
159+
for (int i = 0; i < m; ++i)
160+
{
161+
for (int j = 0; j < n; ++j)
162+
{
163+
if (i == 0 || j == 0)
164+
{
165+
vis1.insert(i * n + j);
166+
q1.emplace(i, j);
167+
}
168+
if (i == m - 1 || j == n - 1)
169+
{
170+
vis2.insert(i * n + j);
171+
q2.emplace(i, j);
172+
}
173+
}
174+
}
175+
bfs(q1, vis1);
176+
bfs(q2, vis2);
177+
vector<vector<int>> ans;
178+
for (int i = 0; i < m; ++i)
179+
{
180+
for (int j = 0; j < n; ++j)
181+
{
182+
int x = i * n + j;
183+
if (vis1.count(x) && vis2.count(x))
184+
{
185+
ans.push_back({i, j});
186+
}
187+
}
188+
}
189+
return ans;
190+
}
191+
192+
void bfs(queue<pii>& q, unordered_set<int>& vis) {
193+
vector<int> dirs = {-1, 0, 1, 0, -1};
194+
while (!q.empty())
195+
{
196+
for (int k = q.size(); k > 0; --k)
197+
{
198+
auto p = q.front();
199+
q.pop();
200+
for (int i = 0; i < 4; ++i)
201+
{
202+
int x = p.first + dirs[i];
203+
int y = p.second + dirs[i + 1];
204+
if (x >= 0 && x < m && y >= 0 && y < n && !vis.count(x * n + y) && heights[x][y] >= heights[p.first][p.second])
205+
{
206+
vis.insert(x * n + y);
207+
q.emplace(x, y);
208+
}
209+
}
210+
}
211+
}
212+
}
213+
};
52214
```
53215

54216
### **...**

0 commit comments

Comments
 (0)