Path With Minimum Effort

medium graph dijkstra matrix

Problem

Given an m × n grid of heights, the effort of a path is the maximum absolute height difference between adjacent cells along it. Return the minimum effort needed to travel from (0,0) to (m-1, n-1).

Inputheights = [[1,2,2],[3,8,2],[5,3,5]]
Output2
A path 1→2→2→2→5 keeps every step within difference 2.

import heapq

def minimum_effort_path(heights):
    m, n = len(heights), len(heights[0])
    eff = [[float('inf')] * n for _ in range(m)]
    eff[0][0] = 0
    h = [(0, 0, 0)]
    while h:
        e, r, c = heapq.heappop(h)
        if (r, c) == (m - 1, n - 1): return e
        if e > eff[r][c]: continue
        for dr, dc in ((1,0),(-1,0),(0,1),(0,-1)):
            nr, nc = r + dr, c + dc
            if 0 <= nr < m and 0 <= nc < n:
                ne = max(e, abs(heights[nr][nc] - heights[r][c]))
                if ne < eff[nr][nc]:
                    eff[nr][nc] = ne
                    heapq.heappush(h, (ne, nr, nc))
    return 0
function minimumEffortPath(h) {
  const m = h.length, n = h[0].length;
  const eff = Array.from({ length: m }, () => new Array(n).fill(Infinity));
  eff[0][0] = 0;
  const pq = [[0, 0, 0]];
  const dirs = [[1,0],[-1,0],[0,1],[0,-1]];
  while (pq.length) {
    pq.sort((a, b) => a[0] - b[0]);
    const [e, r, c] = pq.shift();
    if (r === m - 1 && c === n - 1) return e;
    if (e > eff[r][c]) continue;
    for (const [dr, dc] of dirs) {
      const nr = r + dr, nc = c + dc;
      if (nr < 0 || nr >= m || nc < 0 || nc >= n) continue;
      const ne = Math.max(e, Math.abs(h[nr][nc] - h[r][c]));
      if (ne < eff[nr][nc]) { eff[nr][nc] = ne; pq.push([ne, nr, nc]); }
    }
  }
  return 0;
}
class Solution {
    public int minimumEffortPath(int[][] h) {
        int m = h.length, n = h[0].length;
        int[][] eff = new int[m][n];
        for (int[] r : eff) Arrays.fill(r, Integer.MAX_VALUE);
        eff[0][0] = 0;
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
        pq.offer(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 == m - 1 && c == n - 1) return e;
            if (e > eff[r][c]) continue;
            for (int[] d : dirs) {
                int nr = r + d[0], nc = c + d[1];
                if (nr < 0 || nr >= m || nc < 0 || nc >= n) continue;
                int ne = Math.max(e, Math.abs(h[nr][nc] - h[r][c]));
                if (ne < eff[nr][nc]) { eff[nr][nc] = ne; pq.offer(new int[]{ne, nr, nc}); }
            }
        }
        return 0;
    }
}
int minimumEffortPath(vector<vector<int>>& h) {
    int m = h.size(), n = h[0].size();
    vector<vector<int>> eff(m, vector<int>(n, INT_MAX));
    eff[0][0] = 0;
    priority_queue<array<int,3>, vector<array<int,3>>, greater<>> pq;
    pq.push({0, 0, 0});
    int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    while (!pq.empty()) {
        auto [e, r, c] = pq.top(); pq.pop();
        if (r == m - 1 && c == n - 1) return e;
        if (e > eff[r][c]) continue;
        for (auto& d : dirs) {
            int nr = r + d[0], nc = c + d[1];
            if (nr < 0 || nr >= m || nc < 0 || nc >= n) continue;
            int ne = max(e, abs(h[nr][nc] - h[r][c]));
            if (ne < eff[nr][nc]) { eff[nr][nc] = ne; pq.push({ne, nr, nc}); }
        }
    }
    return 0;
}
Time: O(m · n · log(m · n)) Space: O(m · n)