Tensorum
LeetCode 70 · 1D / Fibonacci

Climbing Stairs

LeetCode 70 · Easy · solution, intuition & step-by-step walkthrough

ways(n) = ways(n-1) + ways(n-2), with ways(0) = ways(1) = 1.

EasyMathDynamic ProgrammingMemoization
0 msruntime · beats 100%
19.4 MBmemory · beats 7%
3approaches, in order
17accepted runs · last 13 Aug 2026

This is how I solved Climbing Stairs (LeetCode 70), an easy problem from the 1D / Fibonacci pattern. Below: the intuition, the approach step by step, my accepted Python code and the 3 versions I wrote on the way, animated test cases and a line-by-line debugger.

Intuition

To land on step n you must have come from step n-1 (a 1-step) or step n-2 (a 2-step). So the number of ways to reach n is just the ways to reach n-1 plus the ways to reach n-2 — the Fibonacci recurrence wearing a staircase costume.

Approach

  1. 1

    Define the state: ways(i) = number of distinct ways to stand on step i.

  2. 2

    Base cases: there is exactly one way to be on step 0 or step 1.

  3. 3

    Transition: the last move was either a 1-step or a 2-step, so add the two smaller answers.

  4. 4

    Top-down: recurse from n and cache each index in a memo so every step is solved once.

  5. 5

    Bottom-up: fill dp[2..n] left to right, then notice each value only needs the previous two and keep just two variables.

How I solved it · from first idea to final

1
Recursion + memoO(n) time · O(n) space

dfs(index) recurses down to the base cases and caches results in a dict.

2
TabulationO(n) time · O(n) space

Fill a dp array from the bottom step upward.

3
Two variablesAcceptedO(n) time · O(1) space

Slide left_prev and prev forward — the dp array collapsed to two cells.

Recursion + memoPythonO(n) time · O(n) space
class Solution:
    def climbStairs(self, n: int) -> int:
        memo = {}
        def dfs(index):
            if index<=1:
                return 1
            if index in memo:
                return memo[index]
            cost = dfs(index-1)+dfs(index-2)
            memo[index]=cost
            return cost
        return dfs(n)
TabulationPythonO(n) time · O(n) space
class Solution:
    def climbStairs(self, n: int) -> int:
        dp = [0]*(n+1)
        dp[0]=1
        dp[1]=1
        for index in range(2,n+1):
            cost = dp[index-1]+dp[index-2]
            dp[index]=cost
        return dp[n]
Two variablesPythonO(n) time · O(1) space
class Solution:
    def climbStairs(self, n: int) -> int:
        """
        left_prev prev.     cur
                  left_prev prev
        """
        left_prev=1
        prev=1
        for index in range(2,n+1):
            cur = prev+left_prev
            left_prev=prev
            prev = cur
        return prev
Loading the step-through…

Climbing Stairs — FAQ

What is the intuition behind Climbing Stairs?

To land on step n you must have come from step n-1 (a 1-step) or step n-2 (a 2-step). So the number of ways to reach n is just the ways to reach n-1 plus the ways to reach n-2 — the Fibonacci recurrence wearing a staircase costume.

How do you solve Climbing Stairs (LeetCode 70)?

1. Define the state: ways(i) = number of distinct ways to stand on step i. 2. Base cases: there is exactly one way to be on step 0 or step 1. 3. Transition: the last move was either a 1-step or a 2-step, so add the two smaller answers. 4. Top-down: recurse from n and cache each index in a memo so every step is solved once. 5. Bottom-up: fill dp[2..n] left to right, then notice each value only needs the previous two and keep just two variables.

What is the time and space complexity of Climbing Stairs?

Time: O(n). Space: O(1) in the final version (O(n) for memo / table). (Accepted approach: Two variables.)

What are common mistakes in Climbing Stairs?

Plain recursion without the memo is exponential. Start the loop at 2 — dp[0] and dp[1] are seeds, not computed values.

Is there more than one way to solve Climbing Stairs?

Recursion + memo (O(n) time · O(n) space): dfs(index) recurses down to the base cases and caches results in a dict. Tabulation (O(n) time · O(n) space): Fill a dp array from the bottom step upward. Two variables (O(n) time · O(1) space): Slide left_prev and prev forward — the dp array collapsed to two cells.