27 · Shortest Paths

#1631 Path With Minimum Effort

Mediumsolve on LeetCode ↗

frequently reported atGoogle

the problem

In plain words

heights is a rows × cols grid. Walk from the top-left to the bottom-right moving up/down/left/right. A route's effort is its largest absolute height difference between two consecutive cells. Return the minimum effort.

in heights = [[1,2,2],[3,8,2],[5,3,5]]

out 2

1→3→5→3→5 never steps more than 2.

in heights = [[1,2,3],[3,8,4],[5,3,5]]

out 1

in 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]]

out 0

  • 1 ≤ rows, cols ≤ 100
  • 1 ≤ heights[i][j] ≤ 10⁶

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #778
      Swim in Rising Water
      Hardin Shortest Paths
    2. #875
      Koko Eating Bananas
      Mediumin Binary Search
    3. #1584
      Min Cost to Connect All Points
      Mediumin Union-Find & MST