AlgoFrames

Write the recurrence

Standard · Fill in the code · about 8 min

Step i of a staircase costs cost[i] to stand on. You start on step 0 or step 1 and climb 1 or 2 steps at a time. The top is just past the last step.

The state is dp[i], the cheapest way to reach step i. Fill the blanks: the base cases, the recurrence and the answer.

Example 1

Input:

[[10,15,20]]

Output:

15

Start on step 1, pay 15, jump 2 to the top.

Example 2

Input:

[[1,100,1,1,1,100,1,1,100,1]]

Output:

6

Constraints

  • 1 ≤ cost.length ≤ 1,000
  • 0 ≤ cost[i] ≤ 999

Learn the idea first: Recognizing DP and choosing the state, a visual lesson.

More Recognizing DP and choosing the state problems