Imagine you're trapped in a flooding valley represented by an n × n grid where each cell contains an elevation value. As time passes, water continuously rises - at time t, any cell with elevation ≤ t becomes submerged and navigable.

Your Goal: Find the minimum time needed to swim from the top-left corner (0,0) to the bottom-right corner (n-1, n-1).

Swimming Rules:

  • You can only move to 4-directionally adjacent cells (up, down, left, right)
  • Both your current cell and destination cell must have elevation ≤ current water level
  • You can swim infinitely fast once a path is available
  • You must stay within grid boundaries

Think of it as finding the lowest "bottleneck" elevation that blocks your escape route!

Input & Output

example_1.py — Basic 2×2 Grid
$ Input: grid = [[0,2],[1,3]]
Output: 3
💡 Note: At time 3, water level is 3. All cells are now accessible (0≤3, 2≤3, 1≤3, 3≤3). We can swim from (0,0) to (1,1) via path: (0,0) → (0,1) → (1,1) or (0,0) → (1,0) → (1,1).
example_2.py — Larger Grid with Bottleneck
$ Input: grid = [[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]]
Output: 16
💡 Note: The optimal path requires going through a cell with elevation 16, which becomes the bottleneck. Even though there are cells with higher elevations, the minimum possible maximum elevation on any path is 16.
example_3.py — Single Cell
$ Input: grid = [[5]]
Output: 5
💡 Note: Edge case: already at destination. The water level must be at least 5 to make the starting cell accessible.

Constraints

  • n == grid.length
  • n == grid[i].length
  • 1 ≤ n ≤ 50
  • 0 ≤ grid[i][j] ≤ n2 - 1
  • All values in grid are unique

Visualization

Tap to expand
Swim in Rising Water - DFS Approach INPUT 2x2 Elevation Grid 0 START 2 1 3 END Darker = Higher Elevation Input Array: grid = [[0,2],[1,3]] (0,0) (0,1) (1,0) (1,1) At time t, cells with elev <= t flood Find min t to reach (n-1, n-1) ALGORITHM STEPS 1 Binary Search on Time Search t in [0, max(grid)] 2 DFS Check Path For each t, run DFS 3 Visit Valid Cells Move if elev <= t 4 Find Min Time Return smallest valid t DFS Trace at t=3: 0 2 1 3 Path: (0,0) --> (1,0) --> (1,1) OK - All cells <= 3 FINAL RESULT Minimum Time Found Answer: 3 Winning Path at t=3: 0 2 1 3 At t=3, water level allows swimming through cells: 0 --> 1 --> 3 OK Bottleneck = 3 Key Insight: The answer is the maximum elevation along the optimal path (the "bottleneck"). Using DFS with binary search, we find the minimum time t where a valid path exists. At t=3, we can traverse 0 --> 1 --> 3 since all values <= 3. At t=2, cell (1,1)=3 is blocked. Time complexity: O(n^2 * log(max)) for binary search + DFS. TutorialsPoint - Swim in Rising Water | DFS with Binary Search Approach
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 25
67.3K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen