TRT
Sign in
mediumAdvancedLeetCode#Dijkstra with max instead of sum

Path with minimum effort

Find the route across a height grid whose largest single climb is as small as possible, with Dijkstra on bottleneck costs.

The Runtime Theory Team1 min read
Taught inDijkstra's algorithmPro — members onlyWith non-negative edge weights, the unsettled vertex with the smallest tentative distance can never be improved, so Dijkstra's algorithm settles vertices one at a time in distance order, using a min-heap.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inheights = [[1, 2, 2], [3, 8, 2], [5, 3, 5]]

out2

inheights = [[1, 2, 3], [3, 8, 4], [5, 3, 5]]

out1

inheights = [[1, 10, 6, 7, 9, 10, 4, 9]]

out9

The effort of a route is the largest absolute height difference between consecutive cells. Return the minimum effort to go from the top-left to the bottom-right cell, moving in four directions.

Before you code

A route's cost is its worst step, not the sum of its steps. The maximum along a path never decreases as the path grows, which is all Dijkstra needs.

Hints

  1. effort[cell] starts at infinity, except 0 at the start.
  2. Relaxing a step: new = max(effort[u], abs(h[u] - h[v])).
  3. Pop cells in order of effort; stop when the target is popped. Binary search on the answer plus BFS also works.

Production & complexity notes

When solving Path with minimum effort on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Dijkstra's algorithm to reinforce the underlying pattern.

What it practices

Dijkstra with a non-additive path cost, from Dijkstra's algorithm.

Not started

Sign in to save your learning progress.

Sign in to save