Skip to content
Decoder

Path With Minimum Effort

Medium

Problem

A hiker walks across a grid of heights, from the top-left cell to the bottom-right cell, moving up, down, left or right. The effort of a route is the largest absolute height difference between two consecutive cells on it. Return the minimum effort of any route.

Example 1

heights = [[1, 2, 2],

Example 2

           [3, 8, 2],

Example 3

Input
[5, 3, 5]]
Output
2
Why
route 1 → 3 → 5 → 3 → 5 has steps of at most 2

Example 4

heights = [[1, 2, 3],

Example 5

           [3, 8, 4],

Example 6

Input
[5, 3, 5]]
Output
1
Why
1 → 2 → 3 → 4 → 5

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.