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

Edge cases to test

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