#778

Swim in Rising Water

Hard Dijkstra (minimax) on a grid

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

At time t the water level is t, and you can swim between adjacent cells whose heights are both at most t. Return the least time to get from the top-left to the bottom-right.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the earliest time you can swim from the top-left to the bottom-right.

Why this works: the time for a path is its highest cell; minimise that with a min-heap Dijkstra.

Example: [[0,2],[1,3]]

best path 0 → 1 → 3 → time 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using plain BFS by steps — the number of moves doesn't matter, only the highest cell on the path.

Solve on LeetCode ↗
Complexity
  • Time: O(n² log n).
  • Space: O(n²).
Topic: Graphs
🎬 Episodes 74–85: video coming soon