Shortest Path with Minimum Effort

medium

You are given an R × C grid heights where heights[r][c] is the height of a cell. Starting at the top-left (0,0) and moving 4-directionally to the bottom-right (R-1, C-1), a route's effort is the maximum absolute height difference between two consecutive cells on it. Return the minimum effort over all routes.

Hints

A route's cost is its single worst step (the max height difference), not a sum.
reachable(t) is monotonic in t — binary-search the smallest feasible effort.
Or run Dijkstra, relaxing with max(current effort, |height diff|) instead of a sum.

Common doubts

If a route exists with every step ≤ t, one also exists for any t' > t, so feasibility is monotonic and binary search applies.
Instead of dist[u] + w, relax with max(effort[u], |Δheight|); the greedy 'settle the smallest' logic still holds since the running max is non-decreasing.
Yes — add edges in increasing difference and stop when start and end connect; the last edge's difference is the answer.

Interview follow-ups

Add the four diagonal neighbours; the algorithm is unchanged.
It's the minimax (bottleneck) shortest path, solvable by Dijkstra-with-max, binary search, or a minimum spanning tree.

Fun facts

  • LeetCode 1631 — the classic 'minimize the maximum edge' path problem.
  • The bottleneck path is an edge of the minimum spanning tree between the two cells.

Asked at

AmazonGoogleMicrosoft
Frequently Sometimes Occasionally
Example 1
Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
Output: 2
A route along the right/bottom keeps every step ≤ 2; the 8 is avoided.
Example 2
Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
Output: 1
A route exists whose largest step is 1.
Constraints

- 1 <= R, C <= 100 - 1 <= heights[r][c] <= 10^6

Solve this problem →