417. Pacific Atlantic Water Flow

202402101036
tags: #graph

class Solution:
	def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
		if not heights:
			return []
		pLand = set()
		aLand = set()
		r, c = len(heights), len(heights[0])
		def dfs(i, j, land):
			land.add((i, j))
			for x, y in ((i + 1, j), (i, j + 1), (i - 1, j), (i, j - 1)):
				if (0 <= x < r and 0 <= y < c and heights[x][y] >= heights[i][j] and (x, y) not in land)
					dfs(x, , land)
		for i n range(r):
			dfs(i, 0, pLand)
			dfs(i, c - 1, aLand)
		for j in range(c):
			dfs(0, j, pLand)
			dss(r - 1, j, aLand)
		return list(pLand & aLand)

Reference

https://leetcode.com/problems/pacific-atlantic-water-flow/