1631. Path With Minimum Effort
Problem
Walk from the top-left to the bottom-right cell of heights, moving up, down, left or right. A route's effort is the largest absolute height difference between two consecutive cells on it. Return the minimum effort.
Example: [[1,2,2],[3,8,2],[5,3,5]] → 2
Constraints: up to 100 × 100.
Effort of a path = max |height difference| along it. Dijkstra with cost = max(cost, |Δh|).
Complexity: O(R·C log(R·C)).
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
R, C = len(heights), len(heights[0])
best = [[float("inf")] * C for _ in range(R)]
best[0][0] = 0
pq = [(0, 0, 0)]
while pq:
e, r, c = heappop(pq)
if (r, c) == (R - 1, C - 1):
return e
if e > best[r][c]:
continue
for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= nr < R and 0 <= nc < C:
ne = max(e, abs(heights[nr][nc] - heights[r][c]))
if ne < best[nr][nc]:
best[nr][nc] = ne
heappush(pq, (ne, nr, nc))
assert Solution().minimumEffortPath([[1, 2, 2], [3, 8, 2], [5, 3, 5]]) == 2
assert Solution().minimumEffortPath([[1, 2, 1, 1, 1], [1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 2, 1, 2, 1], [1, 1, 1, 2, 1]]) == 0
class Solution {
public int minimumEffortPath(int[][] heights) {
int R = heights.length, C = heights[0].length;
int[][] best = new int[R][C];
for (int[] row : best) Arrays.fill(row, Integer.MAX_VALUE);
best[0][0] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
pq.add(new int[]{0, 0, 0});
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int e = cur[0], r = cur[1], c = cur[2];
if (r == R - 1 && c == C - 1) return e;
if (e > best[r][c]) continue;
for (int[] d : dirs) {
int nr = r + d[0], nc = c + d[1];
if (nr < 0 || nc < 0 || nr >= R || nc >= C) continue;
int ne = Math.max(e, Math.abs(heights[nr][nc] - heights[r][c]));
if (ne < best[nr][nc]) { best[nr][nc] = ne; pq.add(new int[]{ne, nr, nc}); }
}
}
return 0;
}
}
type effortHeap [][3]int // (effort, row, col), smallest effort first
func (h effortHeap) Len() int { return len(h) }
func (h effortHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }
func (h effortHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *effortHeap) Push(x any) { *h = append(*h, x.([3]int)) }
func (h *effortHeap) Pop() any {
old := *h
x := old[len(old)-1]
*h = old[:len(old)-1]
return x
}
func minimumEffortPath(heights [][]int) int {
R, C := len(heights), len(heights[0])
best := make([][]int, R)
for i := range best {
best[i] = make([]int, C)
for j := range best[i] {
best[i][j] = math.MaxInt
}
}
best[0][0] = 0
pq := &effortHeap{{0, 0, 0}}
dirs := [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
for pq.Len() > 0 {
cur := heap.Pop(pq).([3]int)
e, r, c := cur[0], cur[1], cur[2]
if r == R-1 && c == C-1 {
return e
}
if e > best[r][c] {
continue // stale entry
}
for _, d := range dirs {
nr, nc := r+d[0], c+d[1]
if nr >= 0 && nr < R && nc >= 0 && nc < C {
diff := heights[nr][nc] - heights[r][c]
if diff < 0 {
diff = -diff
}
if ne := max(e, diff); ne < best[nr][nc] {
best[nr][nc] = ne
heap.Push(pq, [3]int{ne, nr, nc})
}
}
}
}
return 0
}