Skip to the code
cf.
+25 XP

#461 Smoothest Route Across a Heightmap

medium

A hiking app shows a grid of terrain heights. A hiker starts at the top-left cell, must reach the bottom-right cell, and moves up, down, left, or right. The effort of a route is the largest height change between any two consecutive cells on it.

Write smoothestRoute(heights) that returns the smallest effort of any route.

Example 1

Input: heights = [[1,2,4],[3,8,5],[5,4,6]]
Output: 2

Route 1 → 2 → 4 → 5 → 6 along the top and right edges has its biggest step from 2 to 4. Every route has a step of at least 2.

Example 2

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

The start is the end, so no steps are taken.

Constraints

  • 1 <= heights.length, heights[i].length <= 100
  • 1 <= heights[i][j] <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).