2
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:3
# Purpose: find the cells that allow rain flow into the ocean5
# Intuition: start from each border, check cell and neb, if OK, append to res7
# init: res, vis (pac, atl), ROW, COL15
for col in range(COL):16
self.dfs(0, col, pac, heights[0][col], heights)17
self.dfs(ROW - 1, col, atl, heights[ROW - 1][col], heights)20
for row in range(ROW):21
self.dfs(row, 0, pac, heights[row][0], heights)22
self.dfs(row, COL - 1, atl, heights[row][COL - 1], heights)25
for row in range(ROW):26
for col in range(COL):27
if (row, col) in pac and (row, col) in atl:28
res.append([row, col])33
def dfs(self, row, col, vis, prev, heights):34
# hard-code definition36
cur = heights[row][col]40
# inbound, unvisited, increase from ocean42
(0 <= row < len(heights) and 0 <= col < len(heights[0]))44
and ((row, col) not in vis)51
self.dfs(row + 1, col, vis, cur, heights)52
self.dfs(row - 1, col, vis, cur, heights)53
self.dfs(row, col + 1, vis, cur, heights)54
self.dfs(row, col - 1, vis, cur, heights)