#1631

Path With Minimum Effort

Medium Dijkstra on a grid (minimax)

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Walk from the top-left to the bottom-right of a height grid. A path's effort is its largest height difference between consecutive cells. Return the minimum effort.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: a path where the biggest single step is as small as possible.

Why this works: Dijkstra where a path's cost is its maximum step, not its sum.

Example: [[1,2,2],[3,8,2],[5,3,5]]

path 1→3→5→3→5 has steps 2, 2, 2, 2 → effort 2

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Summing the differences like a normal shortest path.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n log(m·n)).
  • Space: O(m·n).
Topic: Graphs
🎬 Episodes 74–85: video coming soon