AlgoFrames

Path with minimum effort

Challenge · Write code · about 25 min

You walk from the top-left to the bottom-right cell of a grid of heights, moving up, down, left or right. The effort of a route is the largest height difference between two neighboring cells on it. Return the smallest effort over all routes.

Example 1

Input:

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

Output:

2

The route 1, 3, 5, 3, 5 has jumps 2, 2, 2, 2.

Example 2

Input:

[[[1,2,3],[3,8,4],[5,3,5]]]

Output:

1

The route 1, 2, 3, 4, 5 has jumps of 1.

Constraints

  • 1 ≤ rows, cols ≤ 50
  • 0 ≤ heights[i][j] ≤ 100

Learn the idea first: Dijkstra's algorithm, a visual lesson.

More Dijkstra's algorithm problems