Input: 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]] → Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Find cells where water can flow to both Pacific (top+left) and Atlantic (bottom+right). Reverse BFS from ocean edges inward.
TimeO(m·n)two BFS passes
SpaceO(m·n)sets + queue
Pacific: 0Atlantic: 0Both: 0Step: 0/11
Pacific
Atlantic
Both Oceans
BFS Frontier
Ready
Press Play to watch reverse BFS discover cells reachable from each ocean, or Step to advance one wave at a time.
TimeO(m·n)visit each cell once
SpaceO(m·n)call stack + sets
Recursive DFS from each ocean border. Same reverse-flow logic: expand to neighbors with height ≥ current. Intersection of two reachability sets.
✎ Whiteboard
3
⌨ Type It — Build Muscle Memory
Practice until you don't need to look. Use the guide comments below as scaffolding. Type the full implementation in the editor. The green highlights are the nuances to burn into memory.