Skip to content

Climbing Stairs

You are climbing a staircase with n steps. Each time you can climb 1 or 2 steps. In how many distinct ways can you climb to the top?

nOutputExplanation
22[1, 1] or [2]
33[1, 1, 1], [1, 2], or [2, 1]
45[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], or [2, 2]
58Follow Fibonacci sequence
  • 1 <= n <= 45
ApproachTimeSpaceDescription
DP ArrayO(n)O(n)Store all values, easy to understand and debug
Fibonacci VariablesO(n)O(1)Only keep last two values, space-optimized
  • Pick DP Array if you want the clearest explanation and debugging capability.
  • Pick Fibonacci Variables if you want to optimize space and understand the Fibonacci connection.
Best For Understanding
DP Array
O(n) time · O(n) space
Space Optimized
Fibonacci Variables
O(n) time · O(1) space
★ Recommended

Build a dynamic programming array where dp[i] represents the number of distinct ways to reach step i.

The recurrence relation is simple: to reach step i, you can either:

  • Come from step i-1 and take 1 step
  • Come from step i-2 and take 2 steps

Therefore: dp[i] = dp[i-1] + dp[i-2]

⏱ Time O(n) Single pass 💾 Space O(n) DP array storage

Input: n = 4

Step idp[i]Ways to reach step i
01Base case (starting point)
11[1]
22[1, 1], [2]
33[1, 1, 1], [1, 2], [2, 1]
45[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2]

Animated walkthrough

How the DP array solution counts paths

Use Next or Autoplay to watch the DP array build step by step, showing how each new value is the sum of the previous two.

Step 1 of 4 Target: 4
Waiting to begin...
Array state
Memory / lookup state

function climbingStairsDpArray(n):
if n <= 1:
return 1
dp = array of size n+1
dp[0] = 1
dp[1] = 1
for i from 2 to n:
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
climbing_stairs_dp_array.py
from typing import List
def climbing_stairs_dp_array(n: int) -> int:
"""
Climb n stairs with dp array approach.
Time Complexity: O(n)
Space Complexity: O(n)
At each step i, we can either:
- Come from step i-1 and take 1 step
- Come from step i-2 and take 2 steps
So dp[i] = dp[i-1] + dp[i-2]
"""
if n <= 1:
return 1
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
if __name__ == "__main__":
print(climbing_stairs_dp_array(3)) # 3 (1+1+1, 1+2, 2+1)
print(climbing_stairs_dp_array(4)) # 5
print(climbing_stairs_dp_array(5)) # 8

Approach 2: Fibonacci Variables (Space-Optimized)

Section titled “Approach 2: Fibonacci Variables (Space-Optimized)”
🎯 Interview Favourite

Instead of storing all n values, we only keep track of the last two values (prev1 and prev2) since dp[i] only depends on dp[i-1] and dp[i-2].

This approach recognizes that the climbing stairs problem follows the Fibonacci sequence.

⏱ Time O(n) Single pass 💾 Space O(1) Only two variables

Input: n = 4

Iterationprev2prev1current = prev1 + prev2Interpretation
Start11Base cases
i=21222 ways to reach step 2
i=32333 ways to reach step 3
i=43555 ways to reach step 4
function climbingStairsFibonacciVariables(n):
if n <= 1:
return 1
prev2 = 1
prev1 = 1
for i from 2 to n:
current = prev1 + prev2
prev2 = prev1
prev1 = current
return prev1
climbing_stairs_fibonacci_variables.py
def climbing_stairs_fibonacci_variables(n: int) -> int:
"""
Climb n stairs with Fibonacci variables approach (space-optimized).
Time Complexity: O(n)
Space Complexity: O(1)
Instead of storing all values in an array, we only keep the last two
values since dp[i] only depends on dp[i-1] and dp[i-2].
"""
if n <= 1:
return 1
prev2 = 1 # dp[0]
prev1 = 1 # dp[1]
for i in range(2, n + 1):
current = prev1 + prev2
prev2 = prev1
prev1 = current
return prev1
if __name__ == "__main__":
print(climbing_stairs_fibonacci_variables(3)) # 3 (1+1+1, 1+2, 2+1)
print(climbing_stairs_fibonacci_variables(4)) # 5
print(climbing_stairs_fibonacci_variables(5)) # 8
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
climbing_stairs_space_optimized.py
def solution(): return 0