HardGraphs · Dijkstra · Union-Find · Binary Search

Swim in Rising Water

grid[i][j] is the height of each square. At time t the water level is t, and you can swim between adjacent squares whose heights are both ≤ t. Return the least time to get from the top-left to the bottom-right.

grid = [[0,1],[2,3]] → 3grid = [[0,1,2,10],[9,14,4,13],[12,3,8,15],[11,5,7,6]] → 8 (path 0,1,2,4,8,7,6)

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

The grid

Darker = higher ground; blue = under water

Time and space complexity

n = grid side, so there are n² cells. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Square grids up to 6×6 using each number from 0 to n² − 1 once.