Minimax Dijkstra
Time O(n² log n) Space O(n²)Heap of (max elevation so far, r, c), starting with grid[0][0]. Pop the smallest; when the corner pops, that's the answer.
import java.util.*;
class Solution {
public int swimInWater(int[][] grid) {
int n = grid.length;
boolean[][] seen = new boolean[n][n];
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
pq.offer(new int[]{grid[0][0], 0, 0});
seen[0][0] = true;
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (true) {
int[] t = pq.poll();
if (t[1] == n - 1 && t[2] == n - 1) return t[0];
for (int[] d : dirs) {
int r = t[1] + d[0], c = t[2] + d[1];
if (r < 0 || c < 0 || r >= n || c >= n || seen[r][c]) continue;
seen[r][c] = true;
pq.offer(new int[]{Math.max(t[0], grid[r][c]), r, c});
}
}
}
}Verdict: Same as Path With Minimum Effort, with cell values as costs.