TRT
Sign in
hardAdvancedLeetCode#Dijkstra on the bottleneck

Swim in rising water

Find the earliest time you can swim across a grid as water rises, by minimizing the highest cell on the path with Dijkstra.

The Runtime Theory Team1 min read
Taught inDijkstra's algorithmPro — members onlyWith non-negative edge weights, the unsettled vertex with the smallest tentative distance can never be improved, so Dijkstra's algorithm settles vertices one at a time in distance order, using a min-heap.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

ingrid = [[0, 2], [1, 3]]

out3

ingrid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]

out16

ingrid = [[0]]

out0

At time t, you can swim between adjacent cells whose elevations are both at most t. Return the least time at which you can reach the bottom-right cell from the top-left.

Before you code

The time a route needs is the highest elevation on it. Minimize the maximum along a path: a bottleneck shortest path.

Hints

  1. Heap entries are (highest so far, r, c), starting with the start cell's elevation.
  2. Moving to a neighbor: max(current, grid[nr][nc]).
  3. The first time the target is popped, its value is the answer.

Production & complexity notes

When solving Swim in rising water on LeetCode (hard), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Dijkstra's algorithm to reinforce the underlying pattern.

What it practices

Bottleneck costs in Dijkstra, from Dijkstra's algorithm.

Not started

Sign in to save your learning progress.

Sign in to save