Path With Minimum Effort - Problem
The Mountain Hiker's Challenge
You're a hiker preparing for an epic mountain adventure! 🏔️ You have a detailed
Your mission: Travel from the top-left corner (0, 0) to the bottom-right corner (rows-1, columns-1) using the path that requires the minimum effort.
You can move up, down, left, or right to adjacent cells. The effort of any path is defined as the maximum absolute difference in heights between any two consecutive cells along that route.
Goal: Find the minimum effort required to complete your hike from start to finish.
Example: If you move from height 5 to height 8, then to height 3, your effort for this path would be max(|5-8|, |8-3|) = max(3, 5) = 5.
You're a hiker preparing for an epic mountain adventure! 🏔️ You have a detailed
heights map represented as a 2D grid where heights[row][col] shows the elevation at each point.Your mission: Travel from the top-left corner (0, 0) to the bottom-right corner (rows-1, columns-1) using the path that requires the minimum effort.
You can move up, down, left, or right to adjacent cells. The effort of any path is defined as the maximum absolute difference in heights between any two consecutive cells along that route.
Goal: Find the minimum effort required to complete your hike from start to finish.
Example: If you move from height 5 to height 8, then to height 3, your effort for this path would be max(|5-8|, |8-3|) = max(3, 5) = 5.
Input & Output
example_1.py — Basic Mountain Path
$
Input:
heights = [[1,2,2],[3,8,2],[5,3,5]]
›
Output:
2
💡 Note:
The path [1,3,5,3,5] has a maximum effort of 2. This is better than the path [1,2,2,2,5] which has maximum effort of 3.
example_2.py — Steep Terrain
$
Input:
heights = [[1,2,3],[3,8,4],[5,3,5]]
›
Output:
1
💡 Note:
The path [1,2,3,4,5] has maximum effort of 1, which is the minimum possible for this terrain.
example_3.py — Single Cell
$
Input:
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]]
›
Output:
0
💡 Note:
This path has no effort required since we can find a route where all consecutive cells have the same height.
Constraints
-
rows == heights.length -
columns == heights[i].length -
1 ≤ rows, columns ≤ 100 -
1 ≤ heights[i][j] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code