wunder beta

📘 How does dynamic programming reuse subproblems?

Overlapping subproblems and optimal substructure—how DP trades a table of answers for exponential re-work.

5
lessons
~25 min
to learn
Adults
level
Start the course →

What you’ll learn

  1. What Dynamic Programming IsDefine dynamic programming and identify the two structural properties a problem must have for DP to apply.Dynamic programming is an algorithm-design technique that solves a problem by combining solutions to overlapping subproblems, computing each subproblem exactly once and storing the result. It applies when a problem has optimal substructure (an optimal solution is built from optimal solutions to subproblems) and overlapping subproblems (the same subproblems recur). DP trades extra memory for dramatically reduced running time, often turning exponential recursion into polynomial-time computation. The technique was named and formalized by Richard Bellman, whose Principle of Optimality underlies the recursive structure.
  2. Memoization and TabulationImplement both top-down (memoized) and bottom-up (tabulated) dynamic programming and reason about their trade-offs.There are two standard ways to realize a DP recurrence: top-down memoization, which keeps the natural recursive structure but caches each result, and bottom-up tabulation, which fills a table in an order that guarantees dependencies are ready. Both compute each distinct subproblem once and yield the same asymptotic running time. Memoization is easy to derive from a recurrence and computes only the subproblems actually reached; tabulation avoids recursion overhead and enables space optimizations. Choosing between them is an engineering decision driven by clarity, the subproblem dependency order, and memory constraints.
  3. Designing a DP Solution Step by StepApply a repeatable five-step procedure to derive a correct DP recurrence and analyze its complexity.Designing a DP solution follows a disciplined sequence: define the subproblem precisely, write the recurrence with base cases, choose an evaluation order, implement it via memoization or tabulation, and analyze time and space. The most common errors are an ambiguous subproblem definition and missing or incorrect base cases. A clear statement of what each table entry means is the anchor that makes the recurrence, the order, and the analysis fall into place. Mastering this procedure lets you attack unfamiliar problems systematically rather than by pattern-matching alone.
  4. Canonical DP PatternsRecognize and apply three classic DP problem patterns: linear sequence DP, the 0/1 knapsack, and edit distance.A small number of recurring patterns cover a large share of DP problems. Linear-sequence DP, exemplified by longest increasing subsequence, builds answers along a one-dimensional index. The 0/1 knapsack pattern introduces a second dimension, a resource budget, and the include-or-exclude decision. The edit-distance pattern aligns two sequences in a two-dimensional grid using insert, delete, and substitute operations. Learning to map a new problem onto one of these patterns is a powerful shortcut, but the underlying recurrence reasoning remains the source of correctness.
  5. Workshop: Build Your DP ArtifactBuild a working dynamic-programming mini artifact by applying the full design procedure to the coin-change minimum-coins problem.In this capstone workshop you build a small but complete DP artifact: a solver for the minimum-coins coin-change problem, which asks for the fewest coins from given denominations that sum to a target amount. You define the subproblem, write the recurrence and base cases, choose a fill order, implement bottom-up tabulation, add reconstruction of the actual coins used, and analyze complexity. The result is a self-contained program plus a short write-up demonstrating each design step. Completing it shows you can move from a problem statement to a correct, analyzed DP solution on your own.

Questions this course answers

Which pair of properties must a problem exhibit for dynamic programming to be the appropriate technique?

DP applies precisely when a problem has optimal substructure (the optimal whole is built from optimal parts) and overlapping subproblems (the same subproblems recur). The greedy-choice property with disjoint subproblems characterizes greedy and divide-and-conquer methods, respectively.

Why does merge sort, despite being recursive, not benefit from dynamic programming?

Merge sort divides the input into disjoint halves that do not overlap, so no subproblem is ever solved twice. Without overlapping subproblems, the central DP advantage, reusing repeated answers, does not exist.

The Principle of Optimality, as stated by Bellman, is best summarized as which of the following?

Bellman's Principle of Optimality states that whatever the initial state and first decision, the remaining decisions must constitute an optimal policy for the state that results. This is the formal basis of optimal substructure, not a statement about immediate reward or memory.

What is the defining mechanism that makes top-down memoization a dynamic programming method rather than plain recursion?

Memoization adds a cache so that each distinct subproblem is computed once and reused thereafter; that reuse is what makes it DP. Explicit ordered tables and recursion elimination describe tabulation, and constant memory is not guaranteed.

Compared with memoization, a genuine advantage of bottom-up tabulation is that it:

Tabulation removes recursion and its stack-depth risk and makes the dependency order explicit, which enables rolling-array space reduction. Its asymptotic time matches memoization, it still stores results, and neither method works without optimal substructure.

The rolling-array optimization is applicable when:

If a stage's value depends only on a bounded set of recent stages, you can keep just those and discard the rest, shrinking space to the dependency window. If a stage depended on all others, you could not discard any rows.

Grounded in trusted sources

  • Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 4th ed. (MIT Press), Dynamic Programming chapter
  • Richard Bellman, Dynamic Programming (Princeton University Press, 1957)
  • Skiena, The Algorithm Design Manual, 3rd ed. (Springer), chapter on Dynamic Programming
  • Kleinberg and Tardos, Algorithm Design (Pearson/Addison-Wesley), chapter on Dynamic Programming
  • Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 4th ed. (MIT Press): Dynamic Programming chapter (longest common subsequence) and the 0/1 knapsack discussion
  • Skiena, The Algorithm Design Manual, 3rd ed. (Springer), chapter on Dynamic Programming (edit distance)

Every Wunder lesson is built from real, reputable sources — never invented.

Related courses

Wunder is a personalized learn-anything platform — tell it any topic and it builds a beautiful, fact-checked course in minutes, with narration, a knowledge check, and a college-style University track.

All topics · Home

© 2026 Wunder Learning LLC · Terms & Privacy