Toll Steps to the Top

Easy · 1-D Dynamic Programming · about 15 min

A hiking trail climbs a cliff on n stone steps. Standing on step i costs a toll of tolls[i]. You start on the ground below step 0 (free) and want to reach the top, which is just past the last step. Each move goes up 1 or 2 steps, so from the ground you can land on step 0 or step 1, and you can reach the top from the last step or the one before it. Reaching the top itself is free.

Return the smallest total toll.

  • 0 <= len(tolls) <= 1000, each toll is between 0 and 1000.
  • With no steps, or only one step (jump straight over it), the answer is 0.

Example: cheapest_climb([10, 15, 20]) → 15 (land on step 1, then jump to the top)

Hints

  • Write down the cheapest cost to stand on each step, from the bottom up.
  • A step is reached from one or two steps below, and the ground costs nothing.
  • You do not have to stand on the last step: the top can be reached from either of the last two.

Learn the pattern first: the 1-D Dynamic Programming lesson.

More 1-D Dynamic Programming problems