Practice: Pacific Atlantic Water Flow

Problem: https://leetcode.com/problems/pacific-atlantic-water-flow/

Recognition reminder: a grid of heights is a graph again, and water flows from a cell to an equal-or-lower neighbor. The naive idea, “from each cell, can I reach both oceans”, is a traversal from every cell and is wasteful. The insight is to reverse the flow: start from the ocean edges and climb to equal-or-higher cells, marking everything each ocean can reach. A cell drains to both oceans exactly when it is in both reachable sets, so the answer is the intersection.

Signature note

Write your solution as a function pacific_atlantic(heights: list[list[int]]) -> list[list[int]]. Return a list of [row, col] coordinates for the cells that can reach both oceans. Order does not matter; the provided test compares your output against the expected set of coordinates (as a set of tuples), so any ordering passes. The Pacific touches the top and left edges; the Atlantic touches the bottom and right edges.

Before you start (the five-beat rhythm)

  1. Your Pattern Card for this week is already written.
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

You run two traversals, each of which visits every cell at most once. Let the grid have rows times columns cells and reason from there.

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?