feat(work): implement Pacific Atlantic Water Flow solution

This commit is contained in:
Prad Nukala committed 2026-09-21 18:05:35 -04:00
1 parent f5f31ba1c3
commit ae1d1ceede
1 file changed
+34
@@ -70,6 +70,40 @@ Constraints:
• 0 <= heights[r][c] <= 10^5
"""
class Solution:
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
rows, cols = len(heights), len(heights[0])
pac, atl = set(), set()
def dfs(r, c, seen, prevHeight):
if (
(r, c) in seen
or r < 0
or c < 0
or r == rows
or c == cols
or heights[r][c] < prevHeight
):
return
seen.add((r, c))
dfs(r + 1, c, seen, heights[r][c])
dfs(r - 1, c, seen, heights[r][c])
dfs(r, c + 1, seen, heights[r][c])
dfs(r, c - 1, seen, heights[r][c])
for c in range(cols):
dfs(0, c, pac, heights[0][c])
dfs(rows - 1, c, atl, heights[rows - 1][c])
for r in range(rows):
dfs(r, 0, pac, heights[r][0])
dfs(r, cols - 1, atl, heights[r][cols - 1])
res = []
for r in range(rows):
for c in range(cols):
if (r, c) in pac and (r, c) in atl:
res.append([r, c])
return res