The Gentlest Hike
Medium · Topological Sort & Shortest Paths · about 25 min
A hiking map is a grid heights with r rows and c columns. You start at the top-left cell and want to reach the bottom-right cell, moving up, down, left or right one cell at a time.
The steepness of a route is the largest absolute height difference between two cells next to each other on that route. Return the smallest possible steepness of a route from start to finish.
1 <= r, c <= 100,1 <= heights[i][j] <= 10^6. A 1 × 1 map has steepness0.
Example: gentlest_hike([[1, 2, 2], [3, 8, 2], [5, 3, 5]]) → 2 (1 → 3 → 5 → 3 → 5 never changes by more than 2).
Hints
- This is a shortest-path problem where the cost of a route is its worst step, not its total.
- Dijkstra still works: when you move to a neighbour, its candidate cost is the larger of your cost and the step you just took.
- Allow all four directions; the best route may go up or left.
Learn the pattern first: the Topological Sort & Shortest Paths lesson.
More Topological Sort & Shortest Paths problems
- Finish Every Training Module (Easy)
- Alarm Across the Campus (Easy)
- Does One Skill Lead to Another? (Easy)
- Flip the Fewest One-Way Signs (Easy)
- Letters of a Made-Up Language (Medium)
- Fewest Terms to Graduate (Medium)
- Budget Trip With Few Layovers (Medium)
- Wire Up the Sensors (Medium)
- Pick the Quietest Depot (Medium)
- Rebuild the Travel Route (Medium)
- Cross Before the Flood (Hard)
- How Many Fastest Routes? (Hard)
- Earliest Launch Day (Hard)
- Toll Roads With Coupons (Hard)