Skip to content

Commit e43f14a

Browse files
committed
feat: Solve pacific-atlantic-water-flow problem
1 parent b60b512 commit e43f14a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
"""
3+
https://www.youtube.com/watch?v=s-VkcjHqkGI
4+
๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์—ฌ์„œ ๋ฐ”๋กœ dfs๋‚˜ bfs๋กœ ๋ถˆ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ง‰๋ง‰ํ–ˆ๋‹ค. ์˜์ƒ์˜ ๋ฐฉ์‹์„ ๋ณด๊ณ  ๊ณต๋ถ€
5+
"""
6+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
7+
rows, cols = len(heights), len(heights[0])
8+
pac, atl = set(), set()
9+
10+
def dfs(r, c, visit, prevHeight):
11+
if ((r, c) in visit or r < 0 or c < 0 or r == rows or c == cols or heights[r][c] < prevHeight):
12+
return
13+
visit.add((r, c))
14+
dfs(r + 1, c, visit, heights[r][c])
15+
dfs(r - 1, c, visit, heights[r][c])
16+
dfs(r, c + 1, visit, heights[r][c])
17+
dfs(r, c - 1, visit, heights[r][c])
18+
19+
20+
for c in range(cols):
21+
dfs(0, c, pac, heights[0][c])
22+
dfs(rows - 1, c, atl, heights[rows-1][c])
23+
24+
for r in range(rows):
25+
dfs(r, 0, pac, heights[r][0])
26+
dfs(r, cols - 1, atl, heights[r][cols - 1])
27+
28+
result = []
29+
for r in range(rows):
30+
for c in range(cols):
31+
if (r, c) in pac and (r, c) in atl:
32+
result.append([r, c])
33+
return results

0 commit comments

Comments
ย (0)