Skip to content
Onsite

Pacific Atlantic Water Flow

MediumP3Coding

A rectangular island is described by a grid heights. The Pacific ocean touches the top and left edges; the Atlantic touches the bottom and right edges. Rain on a cell flows to an adjacent cell (up, down, left or right) whose height is less than or equal to its own, and it can flow into an ocean from any edge cell that borders it.

Return the coordinates [row, col] of every cell from which water can reach both oceans. The pairs may be returned in any order.

Example 1

Input
heights = [[1,2,3],[8,9,4],[7,6,5]]
Output
[[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

Example 2

Input
heights = [[1]]
Output
[[0,0]]

Constraints

  • 1 <= rows, cols <= 200
  • 0 <= heights[i][j] <= 10^5

Hints

Run checks the 3 visible tests, Submit all 8.