Lightest Hiking Route

Easy · 2-D & String DP · about 15 min

A hiking map is a grid of numbers: grid[r][c] is the effort needed to cross that square. You start in the top-left square and finish in the bottom-right one, moving only right or down. The effort of a route is the sum of every square it crosses, including the start and the finish.

Return the smallest possible effort.

  • 1 <= rows, cols <= 100, each effort is between 0 and 100.

Example:

lightest_route([[1, 3, 1], [1, 5, 1], [4, 2, 1]]) → 7 (1 → 3 → 1 → 1 → 1)

Hints

  • You can only arrive at a square from above or from the left.
  • Keep the cheapest effort to reach every square, and add the square's own effort.
  • The top row and left column have only one way in, so fill them first.

Learn the pattern first: the 2-D & String DP lesson.

More 2-D & String DP problems