Path With Minimum Effort

MediumGraphsdijkstra

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are hiking across an rows x cols grid heights, where heights[r][c] is the altitude of a cell. You start at the top-left cell (0, 0) and want to reach the bottom-right cell (rows - 1, cols - 1), moving up, down, left or right. The **effort** of a route is the largest absolute height difference between any two consecutive cells on it. Return the minimum effort over all routes. A 1x1 grid needs effort 0.

Examples

Input: heights = [[1,3,2],[6,9,4],[5,7,3]]
Output: 2
The route 1 -> 3 -> 2 -> 4 -> 3 has steps of 2, 1, 2, 1, so its effort is 2. Every first step from 1 already costs at least 2.
Input: heights = [[4,4,4],[4,4,4]]
Output: 0
All cells are level, so any route costs nothing.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dijkstra problems