You are given a square 2-D matrix of distinct integers grid where each integer grid[i][j] represents the elevation at position (i, j).
Rain starts to fall at time = 0, which causes the water level to rise. At time t, the water level across the entire grid is t.
You may swim either horizontally or vertically in the grid between two adjacent squares if the original elevation of both squares is less than or equal to the water level at time t.
Starting from the top left square (0, 0), return the minimum amount of time it will take until it is possible to reach the bottom right square (n - 1, n - 1).
Example 1:
Input: grid = [[0,1],[2,3]]
Output: 3Explanation: For a path to exist to the bottom right square grid[1][1] the water elevation must be at least 3. At time t = 3, the water level is 3.
Example 2:
Input: grid = [
[0,1,2,10],
[9,14,4,13],
[12,3,8,15],
[11,5,7,6]
]
Output: 8Explanation: The water level must be at least 8 to reach the bottom right square. The path is [0, 1, 2, 4, 8, 7, 6].
Constraints:
grid.length == grid[i].length1 <= grid.length <= 500 <= grid[i][j] < n^2
You should aim for a solution with O((n^2)logn) time and O(n^2) space, where n is the number of rows or columns of the square matrix.
Think of this problem as a graph where each cell represents a node. You can move from one cell to its adjacent cell if the time is greater than or equal to the adjacent cell's elevation. Note that swimming does not cost time, but you may need to wait at a cell for the time to reach the required elevation. What do you notice about the path from (0, 0) to (n - 1, n - 1)? Perhaps a greedy approach would be useful here.
We can observe that the maximum elevation value along the path determines the time taken for that path. Therefore, we need to find the path where the maximum elevation is minimized. Can you think of an algorithm to find such a path from the source (0, 0) to the destination (n - 1, n - 1)? Perhaps a shortest path algorithm could be useful here.
We can use Dijkstra's algorithm. We initialize a Min-heap and a matrix with infinity. We run the algorithm starting from the source (0, 0), and we track the maximum elevation encountered along the paths. This maximum elevation is used as the key for comparison in Dijkstra's algorithm. If we encounter the destination (n - 1, n - 1), we return the maximum elevation of the path that reached the destination.
Before attempting this problem, you should be comfortable with:
This brute force tries every possible path from the top-left to the bottom-right.
While walking, your “time” is not the number of steps — it’s the maximum height value you have stepped on so far, because you must wait until water rises to at least that height to pass those cells.
So for each path:
The DFS explores all routes, keeps a visited grid to avoid looping, and returns the best (minimum) possible maximum-height value among all paths.
dfs from (0, 0) with initial time t = 0.(r, c):t = max(t, grid[r][c]) (time needed to stand on this cell).(r, c) is the destination (n-1, n-1) → return t.(r, c) as visited.(r, c) (backtrack) and return that minimum.class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
n = len(grid)
visit = [[False] * n for _ in range(n)]
def dfs(node, t):
r, c = node
if min(r, c) < 0 or max(r, c) >= n or visit[r][c]:
return 1000000
if r == (n - 1) and c == (n - 1):
return max(t, grid[r][c])
visit[r][c] = True
t = max(t, grid[r][c])
res = min(dfs((r + 1, c), t),
dfs((r - 1, c), t),
dfs((r, c + 1), t),
dfs((r, c - 1), t))
visit[r][c] = False
return res
return dfs((0, 0), 0)Here we turn the problem into a yes/no question:
“If the water level is
t, can I reach the bottom-right?”
At water level t, you are only allowed to step on cells with grid[r][c] <= t.
So we try a DFS that only walks through “allowed” cells.
Then we increase t gradually from the smallest possible height to the largest, and return the first t where reaching the end becomes possible.
minH = minimum value in the grid (smallest possible water level to consider)maxH = maximum value in the grid (guaranteed enough water level)canReach(t) using dfs:(0,0)> t (not flooded enough yet)(n-1, n-1), return true.t from minH to maxH:canReach(t) is true, return t (first possible time).visited and try the next t.maxH.class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
n = len(grid)
visit = [[False] * n for _ in range(n)]
minH = maxH = grid[0][0]
for row in range(n):
maxH = max(maxH, max(grid[row]))
minH = min(minH, min(grid[row]))
def dfs(node, t):
r, c = node
if (min(r, c) < 0 or max(r, c) >= n or
visit[r][c] or grid[r][c] > t):
return False
if r == (n - 1) and c == (n - 1):
return True
visit[r][c] = True
return (dfs((r + 1, c), t) or
dfs((r - 1, c), t) or
dfs((r, c + 1), t) or
dfs((r, c - 1), t))
for t in range(minH, maxH):
if dfs((0, 0), t):
return t
for r in range(n):
for c in range(n):
visit[r][c] = False
return maxHInstead of trying every water level t one by one, we binary search the answer.
Key idea:
t, then you can also reach it at any higher level (t+1, t+2, ...). t, you also cannot reach at any lower level.So the condition “reachable at t” is monotonic → perfect for binary search.
To test a fixed t, run a DFS from (0,0) and only move through cells with height <= t.
low = minimum height in gridhigh = maximum height in gridcanReach(t) using dfs:(0,0)grid[r][c] > tdfs reaches (n-1, n-1), return true, else false.t:mid = (low + high) // 2canReach(mid) is true → try smaller time: high = midlow = mid + 1visited before the next dfs check.low == high, that value is the minimum time needed.class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
n = len(grid)
visit = [[False] * n for _ in range(n)]
minH = maxH = grid[0][0]
for row in range(n):
maxH = max(maxH, max(grid[row]))
minH = min(minH, min(grid[row]))
def dfs(node, t):
r, c = node
if (min(r, c) < 0 or max(r, c) >= n or
visit[r][c] or grid[r][c] > t):
return False
if r == (n - 1) and c == (n - 1):
return True
visit[r][c] = True
return (dfs((r + 1, c), t) or
dfs((r - 1, c), t) or
dfs((r, c + 1), t) or
dfs((r, c - 1), t))
l, r = minH, maxH
while l < r:
m = (l + r) >> 1
if dfs((0, 0), m):
r = m
else:
l = m + 1
for row in range(n):
for col in range(n):
visit[row][col] = False
return rThink of each cell’s height as the earliest time you’re allowed to stand on it (water must be at least that high).
While moving from start to end, the total time of a path is not the sum — it’s the maximum height you ever step on (because water must rise to that max).
So the problem becomes:
Find a path from (0,0) to (n-1,n-1) that minimizes the maximum cell height along the path.
That is exactly what Dijkstra can solve if we define:
(timeSoFar, r, c), where timeSoFar = max height on the path up to (r,c).(grid[0][0], 0, 0).timeSoFar.timeSoFar (this is optimal).newTime = max(timeSoFar, grid[nr][nc])(newTime, nr, nc) into the heap.visited set so each cell is processed once (when it's popped with the best possible timeSoFar).class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
N = len(grid)
visit = set()
minH = [[grid[0][0], 0, 0]] # (time/max-height, r, c)
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
visit.add((0, 0))
while minH:
t, r, c = heapq.heappop(minH)
if r == N - 1 and c == N - 1:
return t
for dr, dc in directions:
neiR, neiC = r + dr, c + dc
if (neiR < 0 or neiC < 0 or
neiR == N or neiC == N or
(neiR, neiC) in visit
):
continue
visit.add((neiR, neiC))
heapq.heappush(minH, [max(t, grid[neiR][neiC]), neiR, neiC])Water level t rises over time. At time t, you’re allowed to step only on cells with height <= t.
So as t increases, more cells become “open” and neighboring open cells form bigger connected regions.
We want the earliest time t when the start cell (0,0) and end cell (N-1,N-1) become part of the same connected component.
DSU (Union-Find) is perfect for this: it quickly merges neighboring open cells and checks if start and end are connected.
(height, r, c) and sort by height (smallest first).dsu for N*N cells (each cell is a node with id = r*N + c).(r,c) becomes "open" at time t = height.<= t, it is already open (or also open now), so union the two cells.start (0) and end (N*N-1) are connected.t where they connect is the answer.t.class DSU:
def __init__(self, n):
self.Parent = list(range(n + 1))
self.Size = [1] * (n + 1)
def find(self, node):
if self.Parent[node] != node:
self.Parent[node] = self.find(self.Parent[node])
return self.Parent[node]
def union(self, u, v):
pu = self.find(u)
pv = self.find(v)
if pu == pv:
return False
if self.Size[pu] < self.Size[pv]:
pu, pv = pv, pu
self.Size[pu] += self.Size[pv]
self.Parent[pv] = pu
return True
def connected(self, u, v):
return self.find(u) == self.find(v)
class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
N = len(grid)
dsu = DSU(N * N)
positions = sorted((grid[r][c], r, c) for r in range(N) for c in range(N))
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for t, r, c in positions:
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < N and 0 <= nc < N and grid[nr][nc] <= t:
dsu.union(r * N + c, nr * N + nc)
if dsu.connected(0, N * N - 1):
return tA common mistake is treating "time" as the number of steps taken. In this problem, time represents the maximum height you must wait for the water to rise to before you can traverse your chosen path. It is not the path length.
When calculating the minimum time, you must include both grid[0][0] and grid[n-1][n-1] in your considerations. The answer is at least max(grid[0][0], grid[n-1][n-1]) since you must be able to stand on both endpoints.
When using binary search, setting the wrong initial bounds can cause issues. The lower bound should be the minimum value in the grid (or at least grid[0][0]), and the upper bound should be the maximum value. Using 0 to n*n-1 works but is less precise.
In both the linear search and binary search DFS approaches, forgetting to reset the visited array between different attempts (for different time values) leads to incorrect results. Each DFS with a new threshold needs a fresh visited state.
When using Kruskal's algorithm with DSU, a frequent bug is incorrectly converting 2D coordinates to 1D indices. The formula r * N + c must be used consistently, and you must ensure neighbors are already "open" (have height <= current time) before attempting to union them.