Skip to content

Commit 3a18c78

Browse files
committed
feat: [Week 09-3] solve pacific atlantic water flow
1 parent 3da4563 commit 3a18c78

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+
"""
2+
Solution:
3+
1) ๊ฐ€์žฅ์ž๋ฆฌ์—์„œ ์‹œ์ž‘ํ•ด์„œ ์–ด๋””๊นŒ์ง€ ์˜ฌ๋ผ๊ฐˆ์ˆ˜์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.
4+
2) ๊ต์ง‘ํ•ฉ์„ ์ฐพ๋Š”๋‹ค.
5+
6+
Time: O(m * n)
7+
Space: O(m * n)
8+
"""
9+
10+
11+
class Solution:
12+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
13+
ROWS, COLS = len(heights), len(heights[0])
14+
pacific, atlantic = set(), set()
15+
16+
def dfs(visit, r, c):
17+
if (r, c) in visit:
18+
return
19+
visit.add((r, c))
20+
21+
for i, j in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]:
22+
if 0 <= i and i < ROWS and 0 <= j and j < COLS:
23+
if heights[i][j] >= heights[r][c]:
24+
dfs(visit, i, j)
25+
26+
for i in range(ROWS):
27+
dfs(pacific, i, 0)
28+
dfs(atlantic, i, COLS - 1)
29+
for i in range(COLS):
30+
dfs(pacific, 0, i)
31+
dfs(atlantic, ROWS - 1, i)
32+
33+
return list(pacific.intersection(atlantic))

0 commit comments

Comments
ย (0)