Swim in Rising Water
Swim in Rising Water is LeetCode 778 (Hard). You get an n × n grid where grid[r][c] is the height of that cell. At time t the water everywhere is at depth t.
- You can move up, down, left or right between two cells only when both heights are at most
t. Swimming any distance takes no time. - Start at the top-left cell and return the least time at which you can reach the bottom-right cell.
- Heights are the distinct numbers from 0 to n² − 1, and n is at most 50.
- n == grid.length
- n == grid[i].length
- 1 <= n <= 50
- 0 <= grid[i][j] < n²
- Each value grid[i][j] is unique.
Intuition
Swimming takes no time, so a route costs only its highest cell: you wait for that one cell and every other cell is already covered. The answer is the route whose highest cell is lowest, a minimax path.
Grow the reached area from the start like a flood, always stepping into the lowest cell on its edge. The highest cell entered so far is the water level.
- Free steps: a cell lower than the level is already under water.
- Forced rises: the level only goes up when every edge cell is higher, and any route out of the reached area must cross one of those edge cells.
When the bottom-right cell is entered, the level is the answer.
When a path's cost is its largest single step rather than the sum, you are looking for a minimax or bottleneck path. A heap that always expands the cheapest edge cell solves it, and so does binary search on the answer with a reachability check.
Approach
Before reading on, decide what a route costs if swimming takes no time. Then decide which cell you should enter next if you want the water level to rise as little as possible. Aim for about n² log n.
Two ways to solve it
Always enter the lowest cell next to the reached area and track the highest one entered.
- One pass: each cell is handled once.
- Stops early: often before the whole grid is seen.
The usual interview answer.
Guess a time t, check with a BFS whether the corner is reachable through cells at most t, and halve the range.
- No heap: only a queue.
- Easy to trust: each check is plain reachability.
Does about 2 log n full searches.
Both run in O(n² log n). The heap version touches each cell once and can stop early, so the steps, code and live demo use it; the binary search version follows the demo.
Put the start cell in a min-heap
Push (grid[0][0], 0, 0) and mark the start as seen. You can only stand on a cell once the water covers it, so the start's own height already counts toward the answer.
Pop the lowest cell and raise the level
Pop the smallest height and set level = max(level, h). The heap holds every cell next to the reached area, so this is the cheapest way to grow it. A cell below the current level adds nothing.
Stop when the bottom-right cell comes out
If the popped cell is (n - 1, n - 1), return level. The level only rose when every cell on the edge of the reached area was at least that high, and every route to the corner crosses that edge, so no route can finish lower.
Push each unseen neighbour by its height
For each of the four neighbours inside the grid that is not yet seen, mark it seen and push it with its own height. A cell's key is its height and never changes, so marking it when it is pushed is safe and keeps duplicates out of the heap.
Code in Python, C++ and Java
Binary search + BFS
If the corner is reachable at time t, it is reachable at every later time, so the answer is the smallest t that passes a reachability check. Search between the higher of the two end cells and n² − 1. Each check is a BFS that only enters cells with height at most t.
Common pitfalls
Adding heights along the route
heapq.heappush(heap, (cost + grid[nr][nc], nr, nc))
heapq.heappush(heap, (grid[nr][nc], nr, nc)) # and level = max(level, h) on pop
Swimming takes no time, so only the highest cell matters. On [[0,2],[1,3]] a sum-based search returns 4 (0 + 1 + 3) instead of 3.
Returning the height of the last cell
if r == n - 1 and c == n - 1:
return hif r == n - 1 and c == n - 1:
return levelThe corner can be lower than a cell you had to pass. On [[0,2],[3,1]] the corner is 1, but every route crosses a 2 or a 3, so the answer is 2, not 1.
Binary search starting from 0
lo = 0
lo = max(grid[0][0], grid[n - 1][n - 1])
The reachability check assumes the start is already under water. On [[3,0],[1,2]] it reports a path at time 2 even though you cannot stand on the start until time 3.
Edge cases
[[0]]The start is the corner, so the first pop returns its height, 0.
You must stand on both, so the answer is at least max(grid[0][0], grid[n-1][n-1]). In [[0,2],[1,3]] the corner forces 3.
Complexity
Swim in Rising Water FAQ
Is this Dijkstra's algorithm?
It is Dijkstra with max in place of +. Because a cell's cost here is simply its own height, the running maximum can live in one variable, which is why the heap holds plain heights. It also matches Prim's algorithm growing a spanning tree from the start.
How is it different from Path With Minimum Effort?
Path With Minimum Effort (LeetCode 1631) charges the height difference between neighbours and takes the largest difference on the route. Here the charge is the height of the cell itself. Both are bottleneck path problems solved by the same heap or binary search.
Can union-find solve it?
Yes. Turn on cells in order of height, join each to its turned-on neighbours, and stop at the first height where the start and the corner share a set. That height is the answer.