← Whiteboard questions

Min Cost Climbing Stairs

Find the cheapest way up stairs where every step has a price

DSA, Easy, 1-D DP

Which structure or algorithm, and what does it cost.

A staircase has a price on every step. You pay the price of a step each time you stand on it. You may start on step 0 or on step 1. From a step you climb one or two steps up. You are finished once you are above the top step. Return the smallest total price you can pay.

Examples

Input prices = [7, 10, 9, 1, 2, 12]
Output 13
Why stand on steps 1, 3 and 4: 10 + 1 + 2. You skip step 2, then climb past step 5 at the end.
Input prices = [5, 5]
Output 5
Why stand on one step, then climb past the top from it.
Input prices = [6, 2]
Output 2
Why edge case: start on step 1 and climb past the top, so step 0 is never paid.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Think about arriving on a step. Which earlier steps could you have come from?

Hint 2 the approach

Work out the cheapest total paid to be standing on each step. Write it for the first few steps and see which earlier steps feed into it.

Hint 3 the key step

Let cost[i] be the cheapest total paid before standing on step i, with step n above the top. Steps 0 and 1 cost nothing. Take the cheaper of two routes.

Walkthrough

Full walkthrough brute force to best

Brute force

Try every path with recursion: from a step, pay its price, then try the next step and the one after. The recursion branches in two at every step, so the number of calls grows like the Fibonacci numbers. For 60 steps it is already over 10^12 calls.

Better method

Memoised recursion. Let best(i) be the cheapest cost to finish from step i. Save each best(i) the first time it is found, so every step is solved once. That is O(n) time and O(n) memory, and the recursion goes n levels deep.

Best method

Work forward. Let cost[i] be the cheapest total paid before standing at step i. Steps 0 and 1 cost 0, because you may start at either. To stand at step i, you came from step i - 1 (and paid prices[i - 1]) or from step i - 2 (and paid prices[i - 2]). So cost[i] is the smaller of cost[i - 1] + prices[i - 1] and cost[i - 2] + prices[i - 2]. Each cost needs only the two before it, so keep two numbers. The answer is cost[n], the step above the top step.

Hand trace

Run on [7, 10, 9, 1, 2, 12]. Step 6 is above the top step. Steps 0 and 1 cost 0. Step 2: the cheaper of 0 + 10 (from step 1) and 0 + 7 (from step 0) is 7. Step 3: the cheaper of 7 + 9 and 0 + 10 is 10. Step 4: the cheaper of 10 + 1 and 7 + 9 is 11. Step 5: the cheaper of 11 + 2 and 10 + 1 is 11. Step 6: the cheaper of 11 + 12 and 11 + 2 is 13. The answer is 13.

Code

def min_price(prices):
    n = len(prices)
    a, b = 0, 0                     # cheapest total paid before step i - 2 and step i - 1
    for i in range(2, n + 1):
        a, b = b, min(b + prices[i - 1], a + prices[i - 2])
    return b

Time and memory

O(n) time, because the loop runs n - 1 times. O(1) memory, because only two numbers are kept.

Common mistakes

  • Reading the answer at the last step instead of past it. You may skip over the last step from step n - 2, so the goal is step n.
  • Forcing the start at step 0. You may start at step 1, so [6, 2] costs 2, not 6.
  • Using plain recursion with no saved answers. It repeats the same work and is far too slow for 100,000 steps.

Related problems

More 1-D DP problems
Min Cost Climbing Stairs: walkthrough and Python solution