Quick Answer

Dynamic programming solves a problem in 2 styles. They are memoization and tabulation. Both store every small answer once. Stored answers cut Fibonacci from billions of calls to 51 values. Use dynamic programming when small problems repeat. Python and C++ code below was executed.

Dynamic programming means solving every small problem once and storing the answer. Repeated small problems get a stored answer and no new work.

Think of a quiz answer you already gave. Your friend asks the same question again and you repeat the answer without redoing the math.

Code, call counts and measured timings follow below. Last section shows how to spot these problems.

What Is Dynamic Programming?

Dynamic programming splits a big problem into smaller problems and solves every small problem only once. Next time you need that small answer you take it from storage.

This habit converts a painfully slow solution into an efficient one. It builds on recursion so learn recursion first. Full reference sits in the dynamic programming lesson.

Why Is Plain Recursion So Slow for Fibonacci?

Fibonacci numbers show the problem clearly because every number is the sum of the 2 numbers before it. Plain recursion for fib(5) needs fib(4) and fib(3). Then fib(4) needs fib(3) again so the same work repeats.

Table below counts calls. Counts come from the plain recursive function in the code section.

InputPlain recursion callsUnique values needed
fib(5)156
fib(30)2,692,53731
fib(50)40,730,022,14751

Stored values fix this. Every Fibonacci number is saved at first use. Repeated work disappears and fib(50) needs only 51 values.

Which Style Fits in Memoization vs Tabulation?

Memoization vs tabulation is a choice of style and not of result. Both reach the same answer.

Memoization retains your recursion and adds a cache. Tabulation drops recursion and fills a table from the smallest answer upward.

Style #1: Memoization (Top Down)

What you get. Your recursive code remains almost the same. Check the cache before any work. Return a stored answer or compute it once and save it.

Style #2: Tabulation (Bottom Up)

What you get. You get a plain loop with no call stack. Fill every cell from cells that are already done. This is the table idea from the knapsack problem.

Choose memoization when the recursion is clearer to you. Choose tabulation when depth could be large because Python stops deep recursion near 1,000 calls by default.

How Do You Code Dynamic Programming in Python and C++?

Python version below has plain recursion for comparison and then both styles. Memo dictionary is created inside the function. Default dictionary arguments get shared between calls.

def fib_plain(n):
    if n <= 1:
        return n
    return fib_plain(n - 1) + fib_plain(n - 2)


def fib_memo(n, memo=None):
    if memo is None:
        memo = {}
    if n <= 1:
        return n
    if n in memo:
        return memo[n]
    memo[n] = fib_memo(n - 1, memo) + fib_memo(n - 2, memo)
    return memo[n]


def fib_table(n):
    if n <= 1:
        return n
    dp = [0] * (n + 1)
    dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    return dp[n]

C++ version below uses tabulation. Type long long stores values up to fib(92) and larger inputs overflow.

#include <iostream>
#include <vector>

long long fibTable(int n) {
    if (n <= 1) return n;
    std::vector<long long> dp(n + 1, 0);
    dp[1] = 1;
    for (int i = 2; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2];
    return dp[n];
}

int main() {
    std::cout << fibTable(10) << " " << fibTable(50) << "\n";
    return 0;
}

Tested. Python 3.13.18 matched known values for fib(0) to fib(10) and all 3 functions agreed on fib(50) at 12,586,269,025. g++ 13.3 compiled the C++ sample with warnings on and printed 55 12586269025. Tests ran in a cloud sandbox.

Timings below use the best of 5 trials in CPython 3.13.18. Plain fib(30) used 3 trials.

FunctionTime for fib(30)
Plain recursion0.0880 s
Memoization0.000004 s
Tabulation0.000001 s

Check yourself. Plain recursion needs 15 calls for fib(5). Stored answers cut that to 6 unique values for the same input.

How Do You Spot a Dynamic Programming Problem?

Look for 2 signals. Overlapping subproblems means the same small problem appears again and again like fib(3). Optimal substructure means the optimal answer to the big problem comes from optimal answers to small ones.

When both signals appear the method fits. Table below lists common problems with their repeating piece.

ProblemRepeating pieceVisual
Fibonaccifib(n) for smaller nFibonacci visualiser
0/1 knapsackTop value for a smaller capacityKnapsack visualiser
Longest common subsequenceMatch of shorter string prefixesLCS visualiser
Coin changeFewest coins for a smaller amountNone

Practice every pattern in the DP practice set. This topic is the last stage of the DSA roadmap.

Which Mistakes Break Dynamic Programming Code?

Table below lists 4 mistakes that beginners make and the fix for every mistake.

MistakeResultFix
Default dictionary as memoCache shared between callsCreate the memo inside the function
Plain recursion for a large inputBillions of repeated callsAdd a cache or build a table
Deep memoization on big inputsRecursionError near 1,000 depthSwitch to a table
Small integer type in C++Overflow after fib(92)Use long long and check limits

Frequently Asked Questions

Can You Explain Dynamic Programming in Plain Words?

It solves a problem by splitting it into smaller problems and solving every one once. Stored answers replace repeated work.

What Is the Difference Between Memoization and Tabulation?

Memoization is top down and uses recursion with a cache. Tabulation is bottom up and fills a table from the smallest answer. Both reach the same result.

Is Dynamic Programming the Same as Recursion?

No. Recursion splits a problem into smaller calls. DP adds storage so every small answer is computed once.

When Should You Use Dynamic Programming?

Use it when a problem has overlapping subproblems and optimal substructure. Both signals together point to this method.

Why Is Dynamic Programming Hard for Beginners?

Many learners meet it before recursion feels solid. Others memorize solutions and never learn the 2 signals. Learn recursion first and then practice spotting the pattern.

What Are Common Dynamic Programming Problems?

Fibonacci numbers, the knapsack problem, longest common subsequence and coin problems are common ones. All of them reuse smaller answers.

What Is the Time Complexity of Fibonacci With Dynamic Programming?

It is O(n) because every value from 0 to n is computed once. Tabulation stores a table of n plus 1 values so space is O(n).

What Should You Remember About This Method?

Smart remembering is the core idea. Solve every small problem once and store the answer.

Key takeaways.

  • Memoization vs tabulation decides the style and both give the same answer.
  • Fibonacci drops from 40,730,022,147 calls at fib(50) to 51 values.
  • Overlapping subproblems and optimal substructure signal a fit.