Skip to content
Decoder

Pacific Atlantic Water Flow

Medium

Problem

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.

Example 1

heights = [[1, 2, 2, 3, 5],

Example 2

           [3, 2, 3, 4, 4],

Example 3

           [2, 4, 5, 3, 1],

Example 4

           [6, 7, 1, 4, 5],

Example 5

           [5, 1, 1, 2, 4]]

Example 6

  →  [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]

Example 7

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.