Min Cost Climbing Stairs solution explained

Min Cost Climbing Stairs is solved with one recurrence: the cheapest way to arrive at position i is the smaller of arriving at i − 1 and paying for that step, or arriving at i − 2 and paying for that one. Fill that from the bottom up and the answer is the value just past the last step, in O(n) time and O(1) extra space with two rolling variables.

By Published 8 min read

The problem in plain terms

Picture a staircase where step i has a toll of cost[i]. You pay a step's toll when you land on it, then you may climb one or two steps from there. You are allowed to begin on step 0 or on step 1 for free, and you are finished once you move beyond the last step (the "top" is the position right after the final index). Return the least total toll.

Examples made for this page
TollsCheapest totalOne cheapest route
[5, 2, 9, 1]3Start on step 1 (2), jump to step 3 (1), step off the top
[10, 15]10Start on step 0 (10), jump two to the top
[3, 8, 2, 7, 4, 6]9Steps 0, 2 and 4 (3 + 2 + 4), then jump two past the end
[1, 100, 1, 1, 100, 1]4Steps 0, 2, 3 and 5, dodging both 100s
Examples made for this page. Scroll sideways on a small screen.

Two details trip people up. First, the top is not the last step; you have to get past it, so the last toll is optional when you can jump from the second-to-last step. Second, starting on step 1 is free, which is not the same as stepping on step 0 and then moving up. Confirm both with the interviewer, and ask what to return for a list with fewer than two entries (zero is the natural answer).

Slow approach: try both moves everywhere

Define go(i) as the cheapest total if you are standing on step i and have to pay for it. Then go(i) = cost[i] + min(go(i + 1), go(i + 2)), and anything at or past the end costs nothing. The answer is min(go(0), go(1)). It is correct, but each call spawns two more, so the number of calls grows like the Fibonacci numbers, roughly 1.6^n. A staircase of 40 steps already means hundreds of millions of calls.

def cheapest_climb_slow(cost):
    n = len(cost)

    def go(i):  # cheapest total from standing on step i, paying for it
        if i >= n:
            return 0
        return cost[i] + min(go(i + 1), go(i + 2))

    return min(go(0), go(1)) if n else 0
Plain recursion: correct, exponential time.

The waste is obvious once you draw the call tree: go(3) is computed from go(1) and again from go(2), and the repeats multiply. Caching each go(i) the first time it is computed (functools.lru_cache in Python, a Map in JavaScript) makes every step cost O(1) and the whole run O(n). That top-down version is a perfectly good interview answer, but recursion depth can hit the default limit in Python for long inputs, which is one reason to go bottom-up.

Main approach: bottom-up dynamic programming

Flip the direction. Let reach[i] be the cheapest total spent before you stand on position i, without paying position i's toll yet. You can be on step 0 or step 1 for free, so reach[0] = reach[1] = 0. To be on position i you came from i − 1 (and paid cost[i − 1]) or from i − 2 (and paid cost[i − 2]). Position n, one past the last index, is the top, and reach[n] is the answer.

def cheapest_climb(cost):
    n = len(cost)
    reach = [0] * (n + 1)  # reach[i]: cheapest spend to stand on position i
    for i in range(2, n + 1):
        reach[i] = min(reach[i - 1] + cost[i - 1], reach[i - 2] + cost[i - 2])
    return reach[n]
Python: the full table, O(n) time and O(n) space.

Each entry only reads the two before it, so you can keep two numbers and slide them forward. That is the version most interviewers hope to see by the end.

def cheapest_climb_lean(cost):
    two_back = one_back = 0  # reach[i - 2] and reach[i - 1]
    for i in range(2, len(cost) + 1):
        two_back, one_back = one_back, min(one_back + cost[i - 1], two_back + cost[i - 2])
    return one_back
Python: two rolling variables, O(1) extra space.
function cheapestClimb(cost) {
  let twoBack = 0;
  let oneBack = 0;
  for (let i = 2; i <= cost.length; i++) {
    const here = Math.min(oneBack + cost[i - 1], twoBack + cost[i - 2]);
    twoBack = oneBack;
    oneBack = here;
  }
  return oneBack;
}
JavaScript: the same rolling update.

Trace [5, 2, 9, 1]. reach starts as 0, 0. Position 2: min(0 + 2, 0 + 5) = 2. Position 3: min(2 + 9, 0 + 2) = 2. Position 4, the top: min(2 + 1, 2 + 9) = 3. The answer is 3, matching the route through steps 1 and 3.

An equivalent formulation stores the cost of standing on step i including its toll: dp[i] = cost[i] + min(dp[i − 1], dp[i − 2]), and the answer is min(dp[n − 1], dp[n − 2]). Both are fine. Pick one, define it in a sentence before writing code, and stick to it; most bugs here come from mixing the two definitions halfway through.

Complexity

n is the number of steps
VersionTimeExtra space
Plain recursionO(φ^n), about 1.6^nO(n) call stack
Recursion with a cacheO(n)O(n) cache plus O(n) stack
Bottom-up tableO(n)O(n)
Two rolling variablesO(n)O(1)
n is the number of steps. Scroll sideways on a small screen.

Edge cases

  • Empty list or a single step: you can start past or on the last step for free, so the answer is 0. Many judges promise at least two steps, but say how your code behaves.
  • Two steps: the answer is the smaller toll, since either one jumps straight to the top.
  • All tolls equal: the answer is about half the steps times the toll, because jumping two at a time is best.
  • Zero tolls: a valid input that should return 0 without special handling.
  • Very long input with the recursive version: Python's default recursion limit is around one thousand frames, so a memoized recursion can crash where the loop does not.

Common mistakes

  • Returning reach[n − 1] or dp[n − 1] only. The top is past the last step, and skipping the last toll is often the whole point.
  • Charging for the starting step twice, or making step 1 reachable only through step 0.
  • Initializing both base cases to cost[0], which quietly forbids starting on step 1.
  • Updating the rolling variables in the wrong order in JavaScript, so the new value overwrites twoBack before it is read. Compute the new value into a temporary first, as above.
  • Not stating the meaning of the DP array out loud. Interviewers grade the definition as much as the loop.

Follow-ups you might hear

  • Return the route, not just the total. Keep the full table and walk back from the top, choosing whichever predecessor produced the minimum.
  • Allow jumps of 1, 2 or 3 steps. The recurrence gains a third term and you keep three rolling values, much like the Tribonacci sequence.
  • Allow jumps of up to k steps. A naive update costs O(k) per step; a monotonic deque that tracks the minimum of the last k values brings it back to O(n).
  • Count the routes instead of minimizing cost. Replace min with addition and you get the classic climbing-stairs counting problem.

Explaining it in an interview

Fictional example

A compact explanation
Candidate
Let reach of i be the cheapest spend to stand on position i before paying its toll. Positions 0 and 1 are free starts, so both are zero.
Candidate
To stand on i, I came from i − 1 and paid its toll, or from i − 2 and paid that one, so I take the min. The top is position n, so reach of n is the answer.
Candidate
That's linear time, and since each value only needs the previous two, I can keep two variables for constant space.

Neighbors in this set

If greedy graph problems are next on your list, Connecting Cities With Minimum Cost is a good contrast: there, taking the cheapest option first is provably safe, while here a cheap step now can force an expensive one later, which is why you need DP. The closest DP sibling is Tribonacci Number, the same rolling-window trick with three values. For a wider list of topics, see algorithm interview questions.

Rehearsing with Natively

For DP drills, a useful habit is to write your recurrence first and only then ask for a hint. Natively can read the problem from a screenshot and suggest an approach, so you can compare its recurrence with yours, and its transcript of a spoken mock lets you check whether you defined the state before coding. More detail is on the AI interview assistant page.

Questions people ask

What is the recurrence for Min Cost Climbing Stairs?

Let reach[i] be the cheapest spend to stand on position i before paying its toll. Then reach[0] = reach[1] = 0 and reach[i] = min(reach[i - 1] + cost[i - 1], reach[i - 2] + cost[i - 2]). The answer is reach[n].

What is the time and space complexity?

O(n) time. A full table uses O(n) space, but each value depends only on the previous two, so two variables bring extra space down to O(1).

Why is the answer not just the cost of reaching the last step?

The goal is the position past the last step. From the second-to-last step you can jump straight to the top, so the last toll is often skipped.

Is greedy enough for Min Cost Climbing Stairs?

No. Always taking the cheaper next step can lead into an expensive one later. Dynamic programming considers both moves at every position.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP