LeetCode 70 · Easy

Climbing Stairs Explained (LeetCode 70)

Updated Nov 27, 2026 · 3 min read

Look at the last move. You reach step n either from step n-1 with a 1-step or from step n-2 with a 2-step, so ways(n) = ways(n-1) + ways(n-2). Start from 1 way for one stair and 2 ways for two, then add your way up keeping only the last two counts: O(n) time and O(1) space.

Watch onYouTubeInstagram

Key points

  • ●Listing every path is exponential: about 2.3 billion calls for 45 stairs.
  • ●Every path ends with a 1-step from n-1 or a 2-step from n-2.
  • ●ways(n) = ways(n-1) + ways(n-2), starting from 1 and 2.
  • ●Keep two numbers, not a table: 43 additions for 45 stairs.
Step-by-step visual sheet: Climbing Stairs
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: Kai's staircase

Kai climbs stairs 1 or 2 steps at a time. Five stairs have 8 different ways up. How many ways are there for 45 stairs?

That's LeetCode 70, Climbing Stairs. You're given n, between 1 and 45, and you count the different sequences of 1-steps and 2-steps that land exactly on the top. Order matters, so 1+2 and 2+1 count as two ways.

The slow way: list every path

Kai's first plan is to try every path. From the bottom he can take a 1-step or a 2-step, and from there he branches again, and again. That's a recursion tree, and it blows up. For 45 stairs it makes about 2.3 billion calls.

The waste is easy to spot once you draw the tree. The question "how many ways from step 3?" gets solved again every time a different branch arrives at step 3.

The idea: look at the very last move

Forget the start. Look at how Kai lands on the top step. He gets there in one of two ways:

  • a 1-step from the stair just below, or
  • a 2-step from two stairs below.

Those two groups don't overlap, and together they cover every path. So:

ways(n) = ways(n-1) + ways(n-2)

Step 3 of the visual sheet above draws this split, with arrows from n-1 and n-2 into n.

Trace it from the bottom

Stairs Ways Built from
1 1 base case
2 2 base case (1+1, 2)
3 3 2 + 1
4 5 3 + 2
5 8 5 + 3

Each number is the two before it, added. Five stairs have 8 ways, which matches what Kai counted. Keep going to 45 and it takes 43 additions.

Two ways to remember answers

You can fix the slow recursion in two ways. One is memoization: keep the recursive function, but save each answer the first time you compute it, so step 3 is only solved once. The other is to build the counts bottom-up, from 1 stair upward, the way the table shows. Both are O(n). The bottom-up version is simpler and doesn't need recursion at all, so that's the one below.

The code in Python

You don't need a whole table. Each new count only uses the previous two, so two variables are enough.

def climb_stairs(n: int) -> int:
    prev_two = 1
    prev_one = 2
    if n == 1:
        return prev_two
    for _ in range(3, n + 1):
        current = prev_one + prev_two
        prev_two, prev_one = prev_one, current
    return prev_one

For n = 5 the loop runs for 3, 4 and 5, producing 3, 5 and 8, and it returns 8. The tuple assignment on the last line of the loop slides both values up at the same time, so you don't overwrite one before you've used it.

Complexity

  • Time: O(n), one addition per stair.
  • Space: O(1), two running counts.

That's exponential down to linear, just by remembering answers you've already worked out.

The trap to watch for

The n = 1 case. The code starts by assuming step 2 exists, with prev_one = 2. If you skip the early return, n = 1 would come back as 2, which is wrong. The sheet's step 7 calls this out.

The other slip is counting sets instead of sequences. If you think 1+2 and 2+1 are the same, you'll get 2 for three stairs instead of 3, and the recurrence won't match your hand count.

This same "split by the last move" idea is how you'll approach House Robber, Decode Ways and Coin Change too. It's the first dynamic programming problem most people meet, and it's worth getting it solid.

Frequently asked questions

Why is Climbing Stairs a Fibonacci problem?

Because each count is the sum of the two before it: 1, 2, 3, 5, 8, and so on. That's the Fibonacci pattern, shifted by one place. It falls out of splitting every path by its final move.

What is the time complexity of Climbing Stairs?

The rolling dynamic programming solution is O(n) time and O(1) space. Plain recursion without memory is exponential, because it solves the same smaller staircases over and over.

Why do the base cases start at 1 and 2?

One stair has one way up: a single 1-step. Two stairs have two ways: 1+1 or 2. Every bigger staircase is built from these two.

Do different orders of the same steps count as different ways?

Yes. 2+1 and 1+2 are two different ways to climb three stairs, which is why three stairs have 3 ways, not 2.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free