Swim in Rising Water

Hard

Topics
GraphBinary SearchHeapMatrix

You are given an n x n grid where grid[i][j] is the elevation at that cell. At time t the water level is t, and you can move between two adjacent cells if both elevations are at most t. Starting at the top-left, return the least time to reach the bottom-right.

Example 1

00211301
At time t you may stand on any cell of height ≤ t. The answer is the smallest t that connects the corners — 3 here — so what matters is the highest cell on the route, not the sum of the route.
2D grid — one row per line.
Input:  grid = [[0,2],[1,3]]
Output: 3

Example 2

00123412423222152121314151631117181920410987601234
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

Constraints

  • 1 <= n <= 50
  • grid holds a permutation of 0 .. n^2 - 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

The cost of a path is the maximum elevation on it, and you want the path that minimizes that maximum. This is a shortest-path where "distance" is a max, so a Dijkstra-style min-heap keyed by the running max works.

Approach

  1. Use a min-heap of (elevation-so-far, r, c) starting at the top-left.
  2. Always pop the cell reachable with the smallest peak elevation.
  3. When you pop the bottom-right, that peak is the answer. Alternatively binary search t + BFS.

Dry Run

grid = [[0, 2],
         [1, 3]]        target is the bottom-right cell

heap entries are (time, row, col). time is the HIGHEST cell on the path so far.

start                heap=[(0,0,0)]
pop (0,0,0)   not the target
              (1,0) holds 1  push (max(0,1), 1,0) = (1,1,0)
              (0,1) holds 2  push (max(0,2), 0,1) = (2,0,1)
                             heap=[(1,1,0) (2,0,1)]
pop (1,1,0)   not the target
              (1,1) holds 3  push (max(1,3), 1,1) = (3,1,1)
                             heap=[(2,0,1) (3,1,1)]
pop (2,0,1)   not the target, (1,1) already seen
                             heap=[(3,1,1)]
pop (3,1,1)   bottom-right   return 3

answer 3

The whole problem is in `max(t, grid[nr][nc])`. A path's cost is its single
worst cell, not the sum of its cells -- so this is Dijkstra with addition
replaced by max. That still works because extending a path can never lower its
worst cell, which is the only property Dijkstra actually needs.

Complexity

  • Time — O(n^2 log n): each cell is pushed once; heap ops are log of the number of cells.
  • Space — O(n^2): the visited set and heap.

Common Pitfalls

  • The path cost is the max cell on the path, not the sum — carry max(t, elevation) in the heap.

Full Solution — all languages

class Solution:
    def swimInWater(self, grid):
        import heapq
        n=len(grid)
        seen={(0,0)}
        heap=[(grid[0][0],0,0)]
        while heap:
            t,r,c=heapq.heappop(heap)
            if r==n-1 and c==n-1: return t
            for dr,dc in ((1,0),(-1,0),(0,1),(0,-1)):
                nr,nc=r+dr,c+dc
                if 0<=nr<n and 0<=nc<n and (nr,nc) not in seen:
                    seen.add((nr,nc)); heapq.heappush(heap,(max(t,grid[nr][nc]),nr,nc))
        return grid[0][0]
SensAI
Beta
Tap the mic to stop voice mode