Data Structures & Algorithms Advanced Graphs
💡
Exercise 121

Swim in Rising Water 30 XP Hard

LeetCode Ctrl+Enter Run Ctrl+S Save

In an n×n grid, at time t you can swim to adjacent cells with elevation ≤ t. Find the minimum time to reach bottom-right from top-left. This is modified Dijkstra where 'distance' is the max elevation along the path.

Use a min-heap. The cost to reach a cell = max(cost to reach current, elevation of neighbor). We want to minimize the maximum elevation.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find minimum time to swim from top-left to bottom-right.
Min-heap of (max_elevation, r, c). Pop smallest, check if destination. Push neighbors with max(current_max, grid[nr][nc]).
🧪 Test Cases
Input
swimInWater(grid)
Expected
3
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.