Pacific Atlantic Water Flow
Find cells whose water can reach both oceans by moving to equal or lower heights.
Why does this pattern fit?
Restate the exact job
Find cells whose water can reach both oceans by moving to equal or lower heights.
Reverse the flow: from each ocean, climb to equal or higher neighbors.
O(mn) time · O(mn) space
Starting from every cell repeats work and complicates two-destination state.
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.