Skip to content

Chapter 9: Dynamic programming

Dynamic programming is recursion plus memoization: don’t solve the same subproblem twice. A naive recursion for Fibonacci, edit distance, or knapsack recomputes the same subproblems an exponential number of times; storing each answer the first time it is computed makes the whole computation polynomial. The idea is simple. The hard part is identifying the state (what defines a subproblem) and the transition (how subproblems relate). If you can write the recurrence, you can write the DP.

Concepts from other sections (3)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

  • Base case: an input on which a recursive function returns directly without calling itself; together with an input that strictly shrinks on every call, it guarantees that the recursion terminates. Read more: Chapter 13: Recursion and divide-and-conquer
  • Binary search tree (BST): a binary tree in which every node’s value is greater than every value in its left subtree and less than every value in its right subtree, so a search follows one root-to-leaf path and an in-order traversal yields the values in sorted order. Read more: Chapter 7: Trees
  • Depth-first search (DFS): traversal that follows one path as far as possible before backtracking, using a stack. Read more: 18.6 Graph as an adjacency list

This is the longest chapter in the book because DP is less one pattern than a family of them, distinguished by the shape of the state. It moves from one-dimensional state over a sequence, through grids and pairs of sequences, to knapsack, intervals, state machines, bitmasks, and trees. Each pattern comes with a template; the chapter then gives a five-step method for approaching any DP problem and walks through seventeen solutions in JavaScript, Python, and C.

Learning objectives

  • Define the state and transition of a DP problem and identify its base cases and evaluation order.
  • Choose between top-down memoization and bottom-up tables, and reduce space with rolling arrays.
  • Apply the linear, grid, two-sequence, knapsack, interval, state-machine, bitmask, and tree DP templates.
  • Explain why 0/1 knapsack iterates capacity backwards and unbounded knapsack forwards.
  • Reconstruct an optimal solution, not just its value, from a DP table.

You should be comfortable with recursion, as in Recursion and divide-and-conquer, and with tree traversal from Trees for the last pattern. Some examples read graph structure, covered in Graphs, and one uses probabilities; Probability and statistics in Mathematics covers the background.

When the answer at position ii depends on the answers at a few earlier positions in a sequence, the state is one index:

dp[i] = best answer using elements 0..i
dp[i] = max(dp[i-1] + something, dp[i-2] + something_else, ...)

Climbing Stairs, House Robber, Maximum Subarray, Decode Ways, Coin Change, and Word Break are all linear DPs. Because each step usually looks back only one or two positions, the table can often be replaced by a few variables.

The most important one-dimensional DP is Kadane’s algorithm for the maximum subarray. The state is the best sum of a subarray ending here: either extend the previous one or start fresh at the current element.

max_ending_here = max_so_far = nums[0]
for x in nums[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)

On a matrix where you move right or down, the state is a cell and the transition comes from the cells above and to the left:

dp[i][j] = best answer reaching cell (i, j)
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]

Unique Paths, Minimum Path Sum, Maximal Square, and Dungeon Game are grid DPs. Since each row depends only on the previous one, a single row of storage is enough.

When the problem relates two strings or arrays, the state is a pair of prefixes:

dp[i][j] = answer for first[0..i-1] and second[0..j-1]
# Transitions usually involve:
# match: dp[i-1][j-1] + something
# skip first: dp[i-1][j] + something
# skip second: dp[i][j-1] + something

Longest Common Subsequence, Edit Distance, Regular Expression Matching, Wildcard Matching, and Interleaving String all take this form. Edit distance is the quintessential two-dimensional DP: each cell is the cheapest of a deletion, an insertion, or a replacement (free when the characters match).

dp[i][j] = min(
dp[i-1][j] + 1, # delete
dp[i][j-1] + 1, # insert
dp[i-1][j-1] + (0 if a[i]==b[j] else 1) # replace
)

To select items under a constraint such as weight or capacity while maximizing value, the state is the items considered so far and the capacity left. In the 0/1 knapsack, each item is used at most once, so each cell either skips item ii or takes it from the previous row:

dp[i][w] = max value using items 0..i with capacity w
dp[i][w] = max(dp[i-1][w], dp[i-1][w - weight[i]] + value[i])

To save space, keep a single row and iterate capacity backwards, so that dp[w - weight] still holds the previous row’s value when you read it. In the unbounded knapsack, items can be reused, so you want the current row’s value, and you iterate capacity forwards:

dp[w] = max value with capacity w (items reusable)
for each item: dp[w] = max(dp[w], dp[w - weight] + value)

0/1 Knapsack, Coin Change (unbounded), Partition Equal Subset Sum, and Target Sum are knapsack problems.

When the optimal strategy is over a contiguous range [i,j][i, j] and you must try every split point, fill the table by increasing interval length so that both halves of every split are already solved:

for length in range(2, n+1):
for i in range(n - length + 1):
j = i + length - 1
for k in range(i, j):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + cost(i, j))

The three nested loops make this O(n3)O(n^3). Matrix Chain Multiplication, Burst Balloons, Minimum Cost Tree From Leaf Values, and the optimal binary search tree are interval DPs.

Some problems have several states at each position: holding a stock, having just sold, resting during a cooldown. Keep one value per state and update them together from the previous position’s values:

held = -prices[0] # holding a stock
sold = 0 # just sold
rest = 0 # cooldown / idle
for price in prices[1:]:
held, sold, rest = max(held, rest - price), held + price, max(rest, sold)

The simultaneous assignment matters: every new value must be computed from the old ones. This template solves Best Time to Buy and Sell Stock with Cooldown, and the same idea covers Best Time to Buy and Sell Stock I through IV and With Transaction Fee.

When there are at most about 20 items and the state must record which subset has been used, encode the subset as the bits of an integer:

dp[mask] = best answer using the subset of items encoded in mask
for mask in range(1 << n):
for i in range(n):
if mask & (1 << i):
dp[mask] = min(dp[mask], dp[mask ^ (1 << i)] + cost(i, mask))

There are 2n2^n masks, which is why nn must be small. The Travelling Salesman Problem, Find the Shortest Superstring, Can I Win, and Partition to K Equal Sum Subsets are bitmask DPs. Math and bit manipulation covers the bit operations.

Finally, for optimization over a tree, each node returns a small tuple of answers for its subtree, typically one for “this node included” and one for “excluded”:

def dp(node):
if not node: return base
left = dp(node.left)
right = dp(node.right)
include_node = f(node.val, left[EXCLUDE], right[EXCLUDE])
exclude_node = g(best(left), best(right))
return (include_node, exclude_node)

House Robber III, Binary Tree Maximum Path Sum, and Binary Tree Cameras use this pattern, which Trees introduced.

Whatever the pattern, the same five steps get you from a problem statement to code:

  1. Define the state. What information do I need to make the optimal decision?
  2. Write the recurrence. How does dp[current] relate to dp[smaller]?
  3. Identify the base cases. What is the answer for the smallest subproblems?
  4. Determine the order. Top-down with memoization, or bottom-up? If bottom-up, which dimensions iterate first?
  5. Optimize space. Can I drop a dimension? Two dimensions often become one by reusing the previous row or a rolling array.

Interviewers favor different variants. As a rough guide:

CompanyFavorite variantDifficulty
Googleinterval DP, bitmask DPHard
Metalinear DP, two-sequence DPMedium-Hard
Amazonknapsack, coin changeMedium
Two SigmaDP-heavy algorithmic roundsHard
Citadelcomplex state-machine DPHard
Jane Streetprobability DP, game-theory DPHard
  • Dynamic programming (DP): solving a problem by breaking it into overlapping subproblems and storing each subproblem’s answer the first time it is computed, which turns an exponential recursion into a polynomial computation.
  • DP state: the parameters that identify one subproblem in dynamic programming, such as an index, a pair of prefixes, or a remaining capacity; choosing it well is the main difficulty of a DP problem.
  • DP transition: the recurrence that expresses a DP state’s answer in terms of smaller states, such as dp[i] = max(dp[i-1], dp[i-2] + nums[i]); together with the base cases it defines the whole computation.
  • Memoization / top-down DP: caching a recursive function’s results by its arguments so each subproblem is solved once; the recursive way to implement dynamic programming.
  • Bottom-up DP / tabulation: filling a table of subproblem answers in an order where each entry’s dependencies are already computed, with no recursion; the iterative way to implement dynamic programming.
  • Rolling array: keeping only the last row or last few values of a DP table when each entry depends only on recent ones, which removes a dimension from the space cost.
  • Kadane’s algorithm: the O(n)O(n) maximum-subarray algorithm that tracks the best sum of a subarray ending at the current position, either extending the previous one or starting fresh, along with the best sum seen overall.
  • 0/1 knapsack: choosing a subset of items, each used at most once, to maximize total value within a capacity; with a single row of storage the capacity loop runs backwards so each item is counted at most once.
  • Unbounded knapsack: the knapsack variant in which each item may be reused any number of times; with a single row of storage the capacity loop runs forwards, and Coin Change is an instance.
  • Edit distance: the minimum number of insertions, deletions, and substitutions that turn one string into another, computed by a two-sequence DP over pairs of prefixes in O(mn)O(mn).
  • Interval DP: dynamic programming over contiguous ranges [i,j][i, j] that tries every split point and fills the table in order of increasing interval length, typically in O(n3)O(n^3); used for matrix-chain multiplication and Burst Balloons.
  • State-machine DP: dynamic programming that keeps one value per discrete state at each position, such as holding, just sold, or resting in stock-trading problems, and updates them together from the previous position’s values.
  • Bitmask DP: dynamic programming whose state includes a subset of up to about 20 items encoded as the bits of an integer, giving 2n2^n states; used for the Travelling Salesman Problem and subset partitioning.

The JavaScript files are solution functions; run them with Node and a small driver, or in an online judge. The Python and C examples are standalone programs: run them with python3 <file>, or compile with cc <file> -o <bin> and run the binary.

The JavaScript solutions, in roughly increasing difficulty:

  • Climbing Stairs: the Fibonacci recurrence f(n)=f(n−1)+f(n−2)f(n) = f(n-1) + f(n-2), kept in two rolling variables after the base cases 11 and 22.
  • House Robber: take-or-skip, where each house either adds its value to the best total two houses back or is skipped. Written as a tail recursion that carries the last two totals.
  • House Robber II: the houses form a circle, so the first and last cannot both be robbed. It runs the linear solution twice, once without the last house and once without the first, and takes the larger.
  • Coin Change: unbounded knapsack, bottom-up. dp[a] is the fewest coins making amount a, initialized to the impossible value amount + 1, and the answer is -1 if it is never improved.
  • Unique Paths: grid DP in a single row; each cell accumulates the paths from the cell to its left and the one above.
  • Decode Ways: partition DP over a digit string. A nonzero digit continues every decoding so far, and a valid two-digit code (10 to 26) adds the decodings from two positions back.
  • Word Break: top-down partition DP with a memo by start index, trying only prefixes up to the longest dictionary word.
  • Maximum Subarray: Kadane’s algorithm.
  • Maximum Product Subarray: Kadane’s idea extended to products by tracking both the running maximum and the running minimum, since multiplying by a negative number swaps them.
  • Longest Common Subsequence: two-string DP using two alternating rows, so space is linear in the length of one string.

The Python and C worked examples:

  • 0/1 knapsack with squashing: extends the binary knapsack with a third choice per item, packing it “squashed” at half its volume (rounded up) for half its profit, and reconstructs the chosen items from the table. Its docstring argues correctness from the standard knapsack: the extension only adds choices, so the profit can never be worse.
  • Edit distance and global alignment: fills the edit-distance table and recovers an alignment of the two strings by tracing back through change, insert, and delete moves, printing gaps as -.
  • Kadane’s algorithm: the maximum subarray in Python, with a short correctness argument in its docstring.
  • DP on a graph: the longest monochromatic path, computed with a DFS whose memo records, for each node, the longest path of its color through its neighbors.
  • Optimal binary search tree: interval DP in C over key access frequencies, keeping a cost table and a root table; main runs it on the frequencies {5, 3, 4, 1, 1, 2, 1}.
  • Probabilistic transitions: a DP over probabilities. Starting from town A and riding a fixed sequence of bus companies, each of which goes to one of its destinations uniformly at random, it computes the probability of ending in each town by pushing probability mass forward one trip at a time.
  • Sequence alignment with reconstruction: alignment in C in O(mn)O(mn) time, recovering the aligned strings by keeping pointers to shared character nodes rather than copying strings. The comments explain how restricting the table to cells with ∣i−j∣≤k|i - j| \le k gives O((m+n)k)O((m+n)k). Given no input it aligns AGATACATCA with GATTAGATACAT.