algorythms
Graph BFS / DFS
LC #417Medium

Pacific Atlantic Water Flow

Graph BFS / DFS
AmazonGoogleMeta

Problem

Find all cells from which water can flow to both the Pacific and Atlantic oceans.

graphdfsbfsmatrix

Constraints

  • 1 ≤ m, n ≤ 200
  • 0 ≤ heights[i][j] ≤ 10⁵

Example

Inputheights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Why

Water flows to a lower or equal neighbor. Pacific: top/left border. Atlantic: bottom/right border

Hints — reveal one at a time