Skip to content
BASA DSA

Pacific Atlantic Water Flow

MediumGraphs

A height grid borders the Pacific on top and left and the Atlantic on bottom and right. Water may move orthogonally to cells of equal or lower height. Return coordinates from which water can reach both oceans, in any order.

Example 1

0,01
0,12
1,04
1,13
Cells show heights. Pacific touches the top and left; Atlantic touches the bottom and right. Water flows to equal or lower neighbors.
Input
heights = [[1,2],[4,3]]
Output
[[0,1],[1,0],[1,1]]

Explanation

Top-right and bottom-left touch both oceans; bottom-right flows to top-right at height 2 and also touches Atlantic boundaries.

Example 2

0,07
Cells show heights. Pacific touches the top and left; Atlantic touches the bottom and right. Water flows to equal or lower neighbors.
Input
heights = [[7]]
Output
[[0,0]]

Explanation

The single cell borders both oceans.

Example 3

0,02
0,12
1,02
1,12
Cells show heights. Pacific touches the top and left; Atlantic touches the bottom and right. Water flows to equal or lower neighbors.
Input
heights = [[2,2],[2,2]]
Output
[[0,0],[0,1],[1,0],[1,1]]

Explanation

Equal heights permit movement across the whole grid, so all cells reach both oceans.

Constraints

  • 1 <= rows, columns <= 200
  • Heights are 0..100000

Function contractpacificAtlanticArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…