AlgoFrames

Min cost climbing stairs

Standard · Write code · about 10 min

You climb a staircase. Leaving step i costs cost[i], and a move climbs 1 or 2 steps. You may start on step 0 or step 1, and the top is the position one past the last step.

Return the minimum total cost to reach the top.

Write down the five slots first: state, transition, base cases, order and answer.

Example 1

Input:

[[10,15,20]]

Output:

15

Example 2

Input:

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

Output:

6

Visit the 1s: steps 0, 2, 3, 4, 6, 7, 9.

Constraints

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

Learn the idea first: The DP recipe: state, transition, base cases, order, answer, a visual lesson.

More The DP recipe: state, transition, base cases, order, answer problems