Skip to content

Pacific Atlantic Water Flow

MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

An island is a grid of heights. The Pacific Ocean touches its top and left edges, the Atlantic touches its bottom and right edges. Rain on a cell can flow to a side neighbour whose height is less than or equal to the current one, and from any edge cell straight into the ocean beside it. Return every [row, col] from which water can reach both oceans, in any order.

heights = [[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]]
  →  [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
 
heights = [[1]]  →  [[0, 0]]

Constraints

  • 1 <= heights.length, heights[i].length <= 200
  • 0 <= heights[i][j] <= 10^5