Dynamic Programming: Remember Work You Already Did

Turn repeated subproblems into a table or cache, starting with a small recurrence you can reason about.

By Rishipal Singh · · 6 min read

A memo table stores each subproblem result so it is reused.
Visual guide: A memo table stores each subproblem result so it is reused.

Dynamic programming is a way to solve problems that reuse the same smaller questions. Instead of calculating an answer repeatedly, solve each subproblem once and remember the result.

Start with a recurrence

Consider the Fibonacci sequence: each value is the sum of the two before it. A straightforward recursive definition repeats work because the same smaller values are calculated many times. A table avoids that repeated calculation by recording each answer as it is found.

Two common styles

Memoization keeps the recursive shape and caches answers on demand. Tabulation builds a table from the smallest cases upward until the target is reached. Both turn repeated work into reuse; the best choice often depends on which subproblems are needed and which style is easier to explain.

A practical checklist

  1. Define exactly what a subproblem's state means.
  2. Write how a state's answer depends on smaller states.
  3. Identify base cases and a valid computation order.
  4. Count the number of states and work per state.

If there are n states and each takes constant work, the time is often O(n). Dynamic programming is not magic; it is careful bookkeeping for overlapping subproblems with a clear recurrence.

Practice: reuse a subproblem

A recursive Fibonacci function computes F(5) and reaches F(3) several times. What does memoization do when F(3) is requested again?

Reveal the explanation

It returns the previously stored F(3) value instead of recursively recomputing it, avoiding repeated work.