Skip to content

Commit 294b4f4

Browse files
authored
Create 417-Pacific-Atlantic-Waterflow.py
1 parent 6ddacad commit 294b4f4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

417-Pacific-Atlantic-Waterflow.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
3+
ROWS, COLS = len(heights), len(heights[0])
4+
pac, atl = set(), set()
5+
6+
def dfs(r, c, visit, prevHeight):
7+
if ((r, c) in visit or
8+
r < 0 or c < 0 or r == ROWS or c == COLS or
9+
heights[r][c] < prevHeight):
10+
return
11+
visit.add((r, c))
12+
dfs(r + 1, c, visit, heights[r][c])
13+
dfs(r - 1, c, visit, heights[r][c])
14+
dfs(r, c + 1, visit, heights[r][c])
15+
dfs(r, c - 1, visit, heights[r][c])
16+
17+
for c in range(COLS):
18+
dfs(0, c, pac, heights[0][c])
19+
dfs(ROWS - 1, c, atl, heights[ROWS - 1][c])
20+
21+
for r in range(ROWS):
22+
dfs(r, 0, pac, heights[r][0])
23+
dfs(r, COLS - 1, atl, heights[r][COLS - 1])
24+
25+
res = []
26+
for r in range(ROWS):
27+
for c in range(COLS):
28+
if (r, c) in pac and (r, c) in atl:
29+
res.append([r, c])
30+
return res

0 commit comments

Comments
 (0)