Problem 526620 · medium · Phase 05 Advanced Algorithms & Graphs

Gentlest Hiking Trail

grids · Dijkstra · minimax path · heap

A hiking map is a grid alt of integer altitudes, alt[r][c] for row r and column c. 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 strain of a route is the largest absolute altitude difference between two consecutive cells on it. Return the smallest possible strain.

Examples

Input:  alt = [[1,3,5],[2,8,4],[6,7,9]]
Output: 4
Explanation: 1 -> 2 -> 6 -> 7 -> 9 has differences 1, 4, 1, 2, so its strain is 4.
             Every route has to make a jump of at least 4 somewhere.

Input:  alt = [[1,10],[10,1]]
Output: 9

Input:  alt = [[7]]
Output: 0

Constraints

  • 1 <= rows, cols <= 80, 0 <= alt[r][c] <= 10**6
  • Target O(R * C * log(R * C)) time.

Goals

  • Recognise a bottleneck (minimax) path problem
  • Adapt Dijkstra's relaxation from a sum to a maximum
  • Run a heap-based search over grid cells
Starting Python…