Start learning free

LESSON 238 OF 314 · DYNAMIC PROGRAMMING

Climbing Stairs in Python: Dynamic Programming in O(n)

Practice O(n) Read the full explanation

You get an integer n, the number of steps in a staircase. Each move takes you up 1 step or 2 steps.

Return how many different ways you can climb from the bottom to exactly the top.

The order of the moves matters. For example, 1 + 2 and 2 + 1 are two different ways.

Example 1:

Input: n = 4
Output: 5
Explanation: The five ways are:
1. 1 + 1 + 1 + 1
2. 1 + 1 + 2
3. 1 + 2 + 1
4. 2 + 1 + 1
5. 2 + 2

Example 2:

Input: n = 1
Output: 1
Explanation: One move of 1 step is the only way.

Example 3:

Input: n = 6
Output: 13
Explanation: Group the ways by how many 2s they use:
no 2s: 1 way (1 + 1 + 1 + 1 + 1 + 1),
one 2: 5 ways (2 + 1 + 1 + 1 + 1 in any order),
two 2s: 6 ways (2 + 2 + 1 + 1 in any order),
three 2s: 1 way (2 + 2 + 2).
In total, 1 + 5 + 6 + 1 = 13.

Constraints:

  • n is from 1 to 45.
  • The answer fits in a 32-bit signed integer.
  • For n = 45, the answer is 1,836,311,903.

STEP 1 OF 6
    Ctrl + Enter to check

    PYTHON · DYNAMIC PROGRAMMING

    Climbing Stairs in Python, explained

    Problem overview

    The method climbStairs receives n, the number of steps in a staircase, and returns how many distinct ways there are to reach the top when each move climbs 1 or 2 steps. Order matters: 1 + 2 and 2 + 1 are different ways. The constraints are 1 <= n <= 45, and the answer fits in 32 bits.

    For n = 4 the answer is 5: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2.

    Approach: build the answer from the smaller staircases

    Listing every sequence of moves works for tiny staircases but the number of sequences grows quickly. The useful observation is about the last move. To stand on step i, you arrived either from step i - 1 with a 1-step move or from step i - 2 with a 2-step move. Those two groups of ways never overlap, so:

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

    That is dynamic programming: each answer reuses two smaller answers, so each is computed once. The recurrence only looks two steps back, so there is no need for a whole table. Two variables are enough.

    Step by step

    1. Create prev = 1, the ways to be on step 0, the ground. There is exactly one way to be there: don't move. Counting it makes step 2 come out as 1 + 1 = 2.
    2. Create curr = 1, the ways to reach step 1: a single 1-step move. For n = 1 this is already the answer.
    3. Loop i over range(2, n + 1), the steps from 2 to n. At the start of each round curr counts step i - 1 and prev counts step i - 2.
    4. Store prev + curr in ways, the count for step i.
    5. Slide the window up one step: prev takes the value of curr, and then curr takes ways. The order matters, because assigning curr first would destroy the value prev needs.
    6. Return curr, which after the last round counts step n.

    The solution in Python

    class Solution:
        def climbStairs(self, n: int) -> int:
            prev = 1
            curr = 1
            for i in range(2, n + 1):
                ways = prev + curr
                prev = curr
                curr = ways
            return curr
    

    Tracing n = 5 shows the pair sliding along. These are the values of prev and curr at the end of each round:

    iprevcurr
    212
    323
    435
    558

    Python details worth knowing:

    • range(2, n + 1) stops before its end, so n + 1 is what makes step n count. For n = 1 the range is empty and the loop body never runs.
    • Python integers have no fixed size, so there is no overflow. The constraints keep the result inside a 32-bit signed integer anyway: climbStairs(45) returns 1836311903.
    • Python can swap and slide in one line: prev, curr = curr, prev + curr evaluates the right side first, so it is equivalent. The lesson's three lines make each move visible.

    Complexity

    Time: O(n), one loop round per step from 2 to n, with constant work in each. Space: O(1), as only prev, curr, ways and i are stored. A recursive version that calls ways(i - 1) + ways(i - 2) without remembering results recomputes the same values again and again and takes exponential time.

    Edge cases and common mistakes

    • Starting prev at 0. Then step 2 would give 0 + 1 = 1 instead of 2. The ground counts as one way.
    • Using range(2, n). The loop stops one step early and returns the answer for n - 1.
    • Updating in the wrong order. Setting curr = ways before prev = curr makes both variables equal.
    • Returning ways. For n = 1 the loop never runs and ways doesn't exist. curr is always defined.

    Testing

    • n = 1 gives 1.
    • n = 2 gives 2.
    • n = 3 gives 3.
    • n = 4 gives 5.
    • n = 10 gives 89.
    • n = 45 gives 1836311903.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free