Back to DSA

Pacific Atlantic Water Flow

medium
Acceptance: 49%
GraphsBFSDFS

An island is represented as an m-by-n height grid bordered by two oceans. The first ocean borders the top and left edges, and the second ocean borders the bottom and right edges. Water flows from higher or equal elevation cells to lower ones in four cardinal directions. Identify all cells from which water can reach both oceans.

Examples

Example 1:
Input:heights = [[2,1],[1,2]]
Output:[[0,0],[0,1],[1,0],[1,1]]
Explanation: All four cells can drain to both oceans.
Example 2:
Input:heights = [[1,2,3],[4,5,6],[7,8,9]]
Output:[[0,2],[1,1],[1,2],[2,0],[2,1],[2,2]]
Explanation: These cells have paths to both the top/left ocean and the bottom/right ocean.

Hints

00:00
1234567