Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Recursion

    Memoization: Don't Solve the Same Small Problem Twice

    Some recursions reach the same smaller problem many times — overlapping subproblems. Naïve Fibonacci was the example (06bigocomplexity.md Ch 8).

    Chapter 7 of 93 min read

    The Plain Picture#

    A teacher asks the class "what's 37 × 49?" many times during a lesson. The first time, you work it out and write the answer on the corner of the board. Every later time, you just read the corner.

    The Real Names#

    • Some recursions reach the same smaller problem many times — overlapping subproblems. Naïve Fibonacci was the example (Big-O and Complexity Ch 8).

    • Memoization = store each subproblem's answer the first time it's computed (in an array or HashMap), and return the stored answer on every later call.

    • This turns many exponential recursions into polynomial ones, and it's the first half of dynamic programming (Dynamic Programming I (coming soon)).

    Under the Hood — "climbing stairs"#

    A student climbs n stairs, taking 1 or 2 steps at a time. In how many different ways can they reach the top? (LeetCode 70 — a real interview problem.)

    1. Smallest: n = 0 or 1 → exactly 1 way.

    2. Smaller: the last move was either 1 step (from stair n−1) or 2 steps (from stair n−2).

    3. Combine: ways(n) = ways(n−1) + ways(n−2).

    Java
    static long stairs(int n) {
        // naïve
        if (n <= 1) return 1;
        return stairs(n - 1) + stairs(n - 2);
    }
    
    static long stairsMemo(int n, long[] memo) {
        // memoized
        if (n <= 1) return 1;
        if (memo[n] != 0) return memo[n]; // already solved → reuse
        return (memo[n] = stairsMemo(n - 1, memo) + stairsMemo(n - 2, memo)); // solve once, store
    }
    Output
    stairs(10) = 89          naive calls=177          memo calls=19
    stairs(30) = 1346269     naive calls=2692537      memo calls=59
    stairs(40) = 165580141   naive calls=331160281    memo calls=79

    For n = 40: 331 million calls → 79 calls, identical answer. Naïve is O(2ⁿ); memoized is O(n) — each stair's answer is computed once. (It's the Fibonacci recurrence in disguise — spotting that a new problem has a known recurrence is a big part of DP.)

    When memoization helps: the same arguments recur (overlapping subproblems) and the answer depends only on the arguments (no hidden state). It doesn't help subsets or permutations — every branch there is a different partial state, so nothing repeats.

    Check yourself

    3 questions
    1

    What are overlapping subproblems?

    Show answer

    The same smaller problem being solved repeatedly within one recursion.

    2

    Why did stairsMemo(40) make only 79 calls?

    Show answer

    Each value from 2 to 40 is computed once (about 2 calls per value, the second returning immediately from the memo).

    3

    Would memoizing subsets speed it up? Why?

    Show answer

    No — every call has a different partial subset, so no subproblem repeats.