Loading problem...
You are planning a trek across a mountainous terrain represented as a two-dimensional grid called heights, where each cell contains the elevation at that position. Starting from the top-left corner (position [0, 0]), your goal is to reach the bottom-right corner (position [rows-1, columns-1]).
From any cell, you can move in four cardinal directions: up, down, left, or right to an adjacent cell. However, traversing between cells requires physical exertion proportional to the elevation change.
The exertion of a complete path is defined as the maximum absolute difference in elevation between any two consecutive cells along that path. Your task is to find a route from the starting position to the destination that minimizes this maximum exertion value.
Return the minimum possible exertion required to complete the journey from the top-left cell to the bottom-right cell.
heights = [[1,2,2],[3,8,2],[5,3,5]]2One optimal path follows the sequence of elevations [1 → 3 → 5 → 3 → 5], where the maximum elevation change between consecutive cells is 2 (from 1 to 3, or from 5 to 3). An alternative route [1 → 2 → 2 → 2 → 5] has a maximum change of 3 (from 2 to 5), which requires more exertion.
heights = [[1,2,3],[3,8,4],[5,3,5]]1The optimal path [1 → 2 → 3 → 4 → 5] traverses cells where no consecutive pair differs by more than 1 in elevation. This is better than the path [1 → 3 → 5 → 3 → 5] which has a maximum difference of 2.
heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]0There exists a path that navigates only through cells with elevation 1, resulting in zero exertion since all consecutive cells have identical elevations.
Constraints