The basic flow logic clicked pretty fast, sort cells by elevation descending, push water forward.
Model the grid as a directed graph where each cell points to its steepest downhill neighbor(s), then process cells in increasing elevation order to accumulate water from higher to lower cells. Use a priority queue to efficiently process cells and handle ties by splitting water equally among multiple steepest neighbors.
Pro tip: Clarify whether water can flow off the grid edges (treat outside as a global minimum) and confirm if local minima can be multiple cells; this shows attention to edge cases and prevents incorrect assumptions.
Ask about grid boundaries, whether water can exit the grid, and if multiple local minima can exist. Confirm that each cell starts with 1 unit of rain and that water splits equally on ties.
For each cell, determine its steepest descent neighbor(s) among 4-directional neighbors. If a cell is a local minimum, it retains its water; otherwise, it has outgoing edges to one or more neighbors.
Since water flows from higher to lower elevations, sort cells by elevation descending (or use a priority queue). Process each cell, distributing its accumulated water equally to its outgoing neighbors.
Initialize each cell with 1 unit of water. As you process cells, add the distributed amounts to neighbors. After processing all cells, the water at each cell is the total collected.
Sorting cells takes O(N log N) where N is the number of cells. Processing each cell and its neighbors is O(N). Overall O(N log N) time and O(N) space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.