PROBLEM 42 OF 75

Pacific Atlantic Water Flow

Find cells whose water can reach both oceans by moving to equal or lower heights.

PATTERNGraphs: traversal & orderingNodes are facts; edges are allowed moves.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Find cells whose water can reach both oceans by moving to equal or lower heights.

WHY THIS FITS

Reverse the flow: from each ocean, climb to equal or higher neighbors.

COMPLEXITY

O(mn) time · O(mn) space

COMMON FAILURE

Starting from every cell repeats work and complicates two-destination state.

THE REASONING, IN ONE PLACE

How to solve Pacific Atlantic Water Flow

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Find cells whose water can reach both oceans by moving to equal or lower heights.

Why Graphs: traversal & ordering fits

Reverse the flow: from each ocean, climb to equal or higher neighbors.

State to maintain

Two reachable sets seeded from ocean borders.

Transition

Traverse inward from each ocean and intersect the two visited sets.

Time and space

O(mn) time · O(mn) space

Counterexample to the tempting mistake

Starting from every cell repeats work and complicates two-destination state.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗