Algo-Sim › Dynamic Programming
Climbing Stairs: Recursion → Memo → Table
Easy · Dynamic Programming · step through it in Java, Python and C++ on your own input
The same recurrence three ways: plain recursion repeats work, memoization solves each n once, tabulation fills the array bottom-up.
Problem
A staircase has n steps, and each move climbs either 1 or 2 of them. Return how many different sequences of moves reach the top. This lesson solves it three ways: plain recursion, recursion with memoization, and a bottom-up table.
Constraints
- 1 ≤ n ≤ 45
- Plain recursion is exponential; memo and table are O(n)
Edge cases to test
- n = 1 and n = 2
- Large n: the count overflows 32 bits after n = 45
- n = 0 (one way: do nothing), if allowed
Follow-up questions
- What if you can climb 1, 2 or 3 steps (or any set)?
- Sum dp over each allowed step:
dp[i] = Σ dp[i − s], O(n·k) time and O(max s) space. - Can you do it in O(log n)?
- It is Fibonacci, so raise the 2 × 2 matrix to the n-th power: O(log n).
Solution
Java
// 1. Plain recursion: exponential, solves the same n again and again.
int climbRec(int n) {
if (n <= 1) return 1;
return climbRec(n - 1) + climbRec(n - 2);
}
// 2. Memoization: the same recursion, but each n is solved once.
int climbMemo(int n, int[] memo) {
if (n <= 1) return 1;
if (memo[n] != 0) return memo[n];
memo[n] = climbMemo(n - 1, memo) + climbMemo(n - 2, memo);
return memo[n];
}
// 3. Tabulation: fill from the base cases up, no recursion at all.
int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}Python
# 1. Plain recursion: exponential, solves the same n again and again.
def climb_rec(n):
if n <= 1:
return 1
return climb_rec(n - 1) + climb_rec(n - 2)
# 2. Memoization: the same recursion, but each n is solved once.
def climb_memo(n, memo):
if n <= 1:
return 1
if n in memo:
return memo[n]
memo[n] = climb_memo(n - 1, memo) + climb_memo(n - 2, memo)
return memo[n]
# 3. Tabulation: fill from the base cases up, no recursion at all.
def climb_stairs(n):
dp = [0] * (n + 1)
dp[0] = dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// 1. Plain recursion: exponential, solves the same n again and again.
int climbRec(int n) {
if (n <= 1)
return 1;
return climbRec(n - 1) + climbRec(n - 2);
}
// 2. Memoization: the same recursion, but each n is solved once.
int climbMemo(int n, vector<int> &memo) {
if (n <= 1)
return 1;
if (memo[n] != 0)
return memo[n];
memo[n] = climbMemo(n - 1, memo) + climbMemo(n - 2, memo);
return memo[n];
}
// 3. Tabulation: fill from the base cases up, no recursion at all.
int climbStairs(int n) {
vector<int> dp = vector<int>(n + 1);
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
};
More Dynamic Programming problems
- Min Cost Climbing Stairs (Easy)
- House Robber (Medium)
- House Robber II (Medium)
- Coin Change (Medium)
- Coin Change II (Medium)
- Perfect Squares (Medium)
- Combination Sum IV (Medium)
- Decode Ways (Medium)
- Maximum Product Subarray (Medium)
- Best Time to Buy and Sell Stock with Cooldown (Medium)
- Best Time to Buy and Sell Stock with Transaction Fee (Medium)
- Best Time to Buy and Sell Stock III (Hard)
- Longest Increasing Subsequence (Medium)
- Word Break (Medium)
- Unique Paths (Medium)
- Unique Paths II (Medium)
- Minimum Path Sum (Medium)
- Maximal Square (Medium)
- Partition Equal Subset Sum (Medium)
- Target Sum (Medium)
- Longest Common Subsequence (Medium)
- Interleaving String (Medium)
- 0/1 Knapsack (Medium)
- Edit Distance (Medium)
- Distinct Subsequences (Hard)
- Longest Palindromic Substring (Medium)
- Palindromic Substrings (Medium)
- Longest Palindromic Subsequence (Medium)
- Predict the Winner (Medium)
- Stone Game (Medium)