Pacific Atlantic Water Flow - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

  • We start from what we already know: cells on an ocean's border can clearly flow into that ocean (they are directly adjacent to it).
  • For each ocean, we:
    • Mark its border cells as able to flow to that ocean,
    • Push them into a queue as our initial "known-good" sources.
  • While traversing, we maintain:
    • a queue β€” containing the cells already known to reach the ocean but not yet dequeued, meaning their unmarked neighbors have not been checked against them;
    • a marked (boolean) matrix β€” canFlowMatrix, indicating which cells are already known to be able to flow to this ocean;
  • From there, we repeatedly expand inward using BFS: when we take a cell (r, c) from the queue (a cell that is already confirmed to reach this ocean), we inspect its 4 neighbors (nr, nc).
  • A neighbor (nr, nc) is also marked as able to reach this ocean and added to the queue if:
    • it is inside the grid,
    • it has not been marked yet for this ocean, and
    • heights[nr][nc] β‰₯ heights[r][c].
    This condition encodes the key idea: if water can flow from (nr, nc) down to (r, c), and (r, c) is already known to reach the ocean, then (nr, nc) can also reach that ocean through (r, c).
  • In other words, we are growing the set of cells that are proven to reach the ocean: we start from confirmed ocean-adjacent cells and, via BFS, keep adding neighbors that can flow into any confirmed cell. The queue always holds cells that are already known to reach the ocean but have not yet been dequeued, so their unmarked neighbors (where canFlowMatrix[neighborRowIndex][neighborColIndex] is not yet true) have not been checked against them. Whenever we just discover that a neighbor can flow to the ocean, we mark it and enqueue it.
  • We run this process separately for:
    • Pacific: starting from top row and left column,
    • Atlantic: starting from bottom row and right column.
    This yields two boolean grids: canFlowToPacific and canFlowToAtlantic.
  • Finally, we scan all cells and collect those where canFlowToPacific[r][c] and canFlowToAtlantic[r][c] are both true. These are exactly the cells that can flow to both oceans.

Time Complexity

O(m Γ— n)
  • Let m be the number of rows and n be the number of columns in the grid.
  • Each cell can be enqueued and processed at most once for the Pacific BFS and at most once for the Atlantic BFS.
  • For each processed cell, we check up to 4 neighbors in O(1) time.
  • Therefore, the total work across both traversals is linear in the number of cells: O(m Γ— n).

Space Complexity

O(m Γ— n)
  • m is the number of rows and n is the number of columns in the grid.
  • We maintain two boolean matrices, canFlowToPacific and canFlowToAtlantic, each of size m Γ— n.
  • The BFS queue holds the cells already known to reach the ocean but not yet dequeued, and its size is maximized during the initialization phase when it contains all edge cells β€” O(m + n).
  • Other auxiliary data structures use constant space.
  • Hence, the total auxiliary space is O(m Γ— n).

Visucodize editorial solution titled JS Solution for LeetCode's Pacific Atlantic Water Flow coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/pacific-atlantic-water-flow.