Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

heights =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Reverse the flow: start from each ocean and move to higher/equal neighbors.

Approach

Run BFS/DFS twice:

  • From all Pacific-border cells.
  • From all Atlantic-border cells. You can traverse from cell A to neighbor B if height(B) >= height(A) (reverse of water flow). Intersect the reachable sets.

Why it works

A cell can flow to an ocean iff the ocean can reach it by reverse edges.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn)
Solution
Updated: 2026-02-23

Idea

Reverse the flow: start from each ocean and move to higher/equal neighbors.

Approach

Run BFS/DFS twice:

  • From all Pacific-border cells.
  • From all Atlantic-border cells. You can traverse from cell A to neighbor B if height(B) >= height(A) (reverse of water flow). Intersect the reachable sets.

Why it works

A cell can flow to an ocean iff the ocean can reach it by reverse edges.

Code
Loading...
Complexity
Time: O(mn)
Space: O(mn)