Skip to main content

417 - Pacific Atlantic Water Flow

Details

KeyValue
Linkhttps://leetcode.com/problems/pacific-atlantic-water-flow/
LanguagePython 3
Runtime368 ms, faster than 74.02% of Python3 online submissions for Pacific Atlantic Water Flow
Memory Usage15.7 MB, less than 55.20% of Python3 online submissions for Pacific Atlantic Water Flow
DatastructuresList[List[int]]
AlgorithmsDFS
ComplexityTime: O(MN) Memory: O(N)

Procedure

  1. ...

Code

class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
if not heights: return []
p_land, a_land = set(), set()
rows, cols = len(heights), len(heights[0])

def spread(row, col, land):
land.add((row, col))
for x, y in (
(row+1, col), (row, col+1),
(row-1, col), (row, col-1)
):
if (
0 <= x < rows and
0 <= y < cols and
heights[x][y] >= heights[row][col] and
(x, y) not in land
):
spread(x, y, land)

for row in range(rows):
spread(row, 0, p_land)
spread(row, cols-1, a_land)
for col in range(cols):
spread(0, col, p_land)
spread(rows-1, col, a_land)

return list(p_land & a_land)