Climbing stairs asks how many different sequences of 1-steps and 2-steps reach stair n.
Video lesson · Chapter 1 of 5
Count the last landing
Split by the last move
Write down a repeated landing
Build the count from the ground
Carry two counts
0:00 / 4:13
Climbing stairs in 75 seconds
1:22·Reel
How it works
Step through the idea, or press Play.
Step 1 of 3
Look at the last move. Stair n was reached by a 1-step from n - 1 or a 2-step from n - 2, never both
Add the two counts. ways(n) = ways(n - 1) + ways(n - 2), with ways(0) = ways(1) = 1 as base cases
Compute each stair once. Fill stairs from the bottom up, keeping only the last two counts: O(n) time, O(1) space
Understand it
Start here
The problem
A staircase has n stairs. Each move climbs either 1 stair or 2 stairs. In how many distinct ways can you reach the top exactly? Two ways are different if the sequence of moves differs, so 1+2 and 2+1 count separately. This is LeetCode 70 and the usual first problem in dynamic programming.
Stairs n
Ways
The sequences
1
1
1
2
2
1+1, 2
3
3
1+1+1, 1+2, 2+1
4
5
1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2
5
8
eight sequences, from 1+1+1+1+1 to 2+2+1
1, 2, 3, 5, 8: each count is the sum of the two above it. That is the whole problem in one column.
A single array of states
In 1D DP each stair i is one state, stored in one cell dp[i], and the cells are filled in one direction.
Dependencies sit behind you
dp[i] only reads dp[i - 1] and dp[i - 2], like a line of dominoes where each one is pushed by the two before it.
Do not try to plan the whole climb. Look only at the final move onto stair n. It was a 1-step from stair n−1 or a 2-step from stair n−2. Every way to reach n−1 extends to n in one way, and so does every way to reach n−2.
Where the last move came from
flowchart TD
A["All ways to stand on stair n"] --> B{"Size of the last move?"}
B -- "1-step" --> C["came from stair n - 1"]
B -- "2-step" --> D["came from stair n - 2"]
C --> E["ways(n - 1) sequences, each plus 1"]
D --> F["ways(n - 2) sequences, each plus 2"]
E --> G["ways(n) = ways(n - 1) + ways(n - 2)"]
F --> G
The two groups never overlap (the last moves differ) and together they cover every sequence, so the counts simply add.
Check with n=4: the 3 paths to stair 3 each get +1, the 2 paths to stair 2 each get +2, total 3+2=5.
Base cases
Why ways(0) = 1
The full definition
ways(n)=⎩⎨⎧11ways(n−1)+ways(n−2)n=0n=1n≥2
Two base cases, because the rule reaches two stairs back.
ways(0) = 1
Zero stairs to climb: you are already there, and doing nothing is one way. It is the empty path every real path starts from. With 0 here, dp[2] would be 1 instead of 2.
ways(1) = 1
One stair: a single 1-step. A 2-step would overshoot, so there is no other option.
Or start at 1 and 2
Many solutions skip stair 0 and use ways(1) = 1, ways(2) = 2. Same sequence, same answers for every n≥1.
The recurrence, typed in directly. Correct, but slow.Edit and run it in your browser
Call tree of ways(5)
flowchart TD
A["w(5)"] --> B["w(4)"]
A --> C["w(3) again"]
B --> D["w(3)"]
B --> E["w(2) again"]
D --> F["w(2)"]
D --> G["w(1)"]
F --> H["w(1)"]
F --> I["w(0)"]
E --> J["w(1)"]
E --> K["w(0)"]
C --> L["w(2) again"]
C --> M["w(1)"]
L --> N["w(1)"]
L --> O["w(0)"]
classDef rep fill:#fde2e2,stroke:#e05a5a,color:#7a1f1f
class C,E,L,J,K,M,N,O rep
15 calls for 6 distinct questions. Everything in red repeats work done elsewhere in the tree.
Call
Times made inside ways(5)
ways(5)
1
ways(4)
1
ways(3)
2
ways(2)
3
ways(1)
5
ways(0)
3
The repeat counts are Fibonacci numbers too: the waste grows at the same rate as the answer.
Complexity
Why brute force is exponential
Count the calls
C(n)=C(n−1)+C(n−2)+1,C(0)=C(1)=1⟹C(n)=2ways(n)−1
Check: C(5)=2⋅8−1=15, C(20)=2⋅10946−1=21,891, and C(45)≈3.67 billion.
Each extra stair multiplies the work by about 1.618. Quoting O(2n) is a correct upper bound.
Space is only O(n): a branch finishes and is popped before its sibling starts, so the call stack holds one root-to-leaf path at a time, at most n frames.
Approach 2
Memoisation: top-down DP
One memoised call
flowchart TD
A["ways(n) is called"] --> B{"n in memo?"}
B -- yes --> C["Memo hit: return memo[n]"]
B -- no --> D{"n <= 1?"}
D -- yes --> E["return 1"]
D -- no --> F["memo[n] = ways(n - 1) + ways(n - 2)"]
F --> G["return memo[n]"]
Each stair reaches the bottom box once. For n=5 that is 9 calls instead of 15; in general 2n−1 calls, O(n) time.
Python
from functools import cachedefways_memo(n, memo=None):if memo isNone: memo = {} # never memo={} in the signatureif n in memo:return memo[n]if n <= 1:return1 memo[n] = ways_memo(n - 1, memo) + ways_memo(n - 2, memo)return memo[n]@cachedefways_cached(n):return1if n <= 1elseways_cached(n - 1) + ways_cached(n - 2)print(ways_memo(45), ways_cached(90)) # 1836311903 4660046610375530309
A dictionary memo, and the same thing with the standard library.Edit and run it in your browser
Approach 3
Tabulation: a 1D dp array
i
0
1
2
3
4
5
dp[i]
1
1
2
3
5
8
how
base
base
1 + 1
2 + 1
3 + 2
5 + 3
Filled left to right for n=5. Each cell reads the two cells before it, which are always ready.
The bottom-up loop
flowchart TD
A["dp = array of n + 1 cells"] --> B["dp[0] = 1, dp[1] = 1"]
B --> C["i = 2"]
C --> D{"i <= n?"}
D -- yes --> E["dp[i] = dp[i - 1] + dp[i - 2]"]
E --> F["i = i + 1"]
F --> D
D -- no --> G["return dp[n]"]
No recursion and no stack: n−1 additions, O(n) time and O(n) space for the array.
Python
defways_table(n):if n <= 1:return1 dp = [0] * (n + 1) dp[0] = dp[1] = 1for i inrange(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2]return dp[n]print([ways_table(n) for n in (3, 5, 7)]) # [3, 8, 21]
Bottom-up with the full array, the version the visualizer steps through.Edit and run it in your browser
Approach 4
O(1) space: keep two numbers
Only a window of two cells is ever read
dp[i]=dp[i−1]+dp[i−2]⟹(a,b)←(b,a+b)
Keep a=dp[i−2] and b=dp[i−1]; one update slides the pair up a stair.Sliding the pair for n = 7
1a, b = 1, 2ways(1), ways(2)
2i = 32, 3
3i = 43, 5
4i = 55, 8
5i = 68, 13
6i = 713, 21: return b
Five updates and two numbers of memory give ways(7) = 21.
Python
defclimb_stairs(n):if n <= 2:return n # 1 -> 1, 2 -> 2 a, b = 1, 2# ways(1), ways(2)for _ inrange(3, n + 1): a, b = b, a + b # right side is computed firstreturn bprint([climb_stairs(n) for n inrange(1, 11)]) # [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
The interview answer: O(n) time, O(1) space.Edit and run it in your browser
Two variables
O(1) extra space, whatever n is.
No recursion, so no recursion limit.
Same O(n) time as the array.
Full dp array
O(n) space for values read once and then forgotten.
Only worth keeping if you need every count, not just the last.
Easy to index past the end when n=0.
The connection
It is Fibonacci, shifted by one
Same rule, different start
ways(0)=1=F(1),ways(1)=1=F(2)⟹ways(n)=F(n+1)
Both sequences add the two previous terms, and the stairs sequence starts one place later. See Recursion: Fibonacci for the call tree in depth.
n
0
1
2
3
4
5
6
7
8
F(n)
0
1
1
2
3
5
8
13
21
ways(n)
1
1
2
3
5
8
13
21
34
The ways row is the Fibonacci row moved one column to the left.
A closed form, and why not to use it
ways(n)=F(n+1)=5φn+1−ψn+1,ψ=21−5≈−0.618
Binet's formula is exact on paper, but floating-point rounding makes it wrong past about n=70.
Generalise
1 to k steps, step sets and costs
Any set of allowed step sizes S
ways(n)=s∈S,s≤n∑ways(n−s),ways(0)=1
S={1,2} is the classic problem. S={1,2,3} gives ways(n)=ways(n−1)+ways(n−2)+ways(n−3), the Tribonacci numbers. For S={1,…,k} keep a running sum of the last k cells to stay O(n).
Steps allowed
n = 1
2
3
4
5
6
{1}
1
1
1
1
1
1
{1, 2}
1
2
3
5
8
13
{1, 2, 3}
1
2
4
7
13
24
{1, 3}
1
1
2
3
4
6
Each row follows the sum rule with its own set of sizes.
Cost variant: minimise instead of count
best(i)=cost(i)+min(best(i−1),best(i−2))
Same two arrows into each stair, but you pay for stairs and keep the cheaper arrival. That is min cost climbing stairs. Counting adds; optimising takes min or max.
Min cost climbing
Pay cost[i] to stand on stair i; take the cheaper of the two ways in. Open the topic.
House robber
Rob house i plus the best up to i−2, or skip it: the same two-back shape with a max. Open the topic.
Decode ways
Read one digit or two digits at a time: climbing stairs where some 1- or 2-steps are not allowed.
Domino tiling of 2 × n
The last piece is one vertical domino or two horizontal ones: exactly ways(n).
Pattern spotting
Choosing an approach
From problem statement to code
flowchart TD
A["How many ways to reach a target?"] --> B{"Fixed set of move sizes?"}
B -- no --> C["Try another model: graph or 2D DP"]
B -- yes --> D["ways(n) = sum of ways(n - s)"]
D --> E{"n small and just exploring?"}
E -- yes --> F["Memoised recursion"]
E -- no --> G["Bottom-up loop"]
G --> H{"Only the last k values read?"}
H -- yes --> I["Keep k variables: O(1) space"]
H -- no --> J["Keep the dp array"]
For climbing stairs every branch ends at the two-variable loop.
Approach
Time
Space
Notes
Brute recursion
Θ(φn), quoted O(2n)
O(n) stack
Lists every path; times out past n≈35
Memoisation (top-down)
O(n)
O(n) memo + stack
Keeps the recursive shape; depth limit applies
Tabulation (bottom-up)
O(n)
O(n)
Keeps every count
Two variables
O(n)
O(1)
The standard answer
Matrix power
O(logn)
O(1)
Only for huge n with a modulus
The space-optimised loop has the same time as tabulation; only the memory changes.
Recap
Key takeaways
Key points
Split by the last move: ways(n) = ways(n - 1) + ways(n - 2) for n ≥ 2.
Base cases ways(0) = 1 (the empty path) and ways(1) = 1, or equivalently ways(1) = 1 and ways(2) = 2.
The answers 1, 1, 2, 3, 5, 8 are Fibonacci shifted by one: ways(n) = F(n + 1).
Brute recursion makes 2·ways(n) - 1 calls: Θ(φⁿ) time, O(n) stack.
Memoisation and tabulation compute each stair once: O(n) time, O(n) space.
Only the last two values are read, so two variables give O(1) space.
With steps 1 to k, add the last k cells; with costs, take a min instead of a sum.