DP

Dynamic Programming

Overlapping subproblems, optimal substructure, memoization and tabulation.

Dynamic Programming
▶ viz

Solve a problem by defining subproblems whose answers are reused, so exponential recursion collapses to polynomial time.

O(states × transition cost) · O(states), often reducible to O(1 row) space
Memoization (Top-Down DP)
▶ viz

Write the natural recursion, then cache every result by its arguments so each distinct subproblem is computed once.

O(states × transition cost) · O(states) memo + O(recursion depth) stack space
Tabulation (Bottom-Up DP)
▶ viz

Fill a table of subproblem answers in an explicit order from base cases upward, with loops instead of recursion.

O(states × transition cost) · O(states), reducible to O(window of dependencies) space
1D (Linear) DP
▶ viz

State is a single index into a sequence; dp[i] is the best answer for the prefix (or suffix) ending at i.

O(n) with O(1) transitions; O(n²) when each state scans all earlier states · O(n), usually reducible to O(1) space
2D (Two-Sequence) DP
▶ viz

State is a pair of prefix lengths (i, j) over two sequences; dp[i][j] combines answers for shorter prefixes of each.

O(n·m) · O(n·m), reducible to O(min(n, m)) space
State Machine DP
▶ viz

State is (position, small status flag); transitions are the edges of a tiny automaton evaluated once per input element.

O(n · k²) for k statuses (O(n · k) when the automaton is sparse) · O(k) space
Grid DP
▶ viz

State is a cell (r, c); the answer for a cell comes from its allowed predecessor cells (usually up and left).

O(rows × cols) · O(rows × cols), reducible to O(cols) space
Knapsack DP
▶ viz

State is (items considered, capacity used); choose items to maximize value or count/decide subsets hitting a target sum.

O(n · W) · O(W) with a rolled row (O(n · W) if reconstruction is needed) space
Subsequence DP
▶ viz

State is "best subsequence ending at index i"; transition scans all earlier j that can precede i.

O(n²) generic; O(n log n) for LIS-type orderings via binary search or Fenwick tree · O(n) space
Interval (Range) DP
▶ viz

State is a contiguous range [l, r]; the answer is built by choosing a split point or the last element removed inside the range.

O(n³) with a split-point transition; O(n²) with an O(1) transition · O(n²) space
Tree DP

State is a node (plus a small flag); each node combines the answers of its children in post-order.

O(n · k) for k statuses per node (O(n) typically); O(n · K²) for subtree-knapsack merges with the small-to-large bound · O(n) table + O(height) recursion space
Bitmask DP
▶ viz

State is a bitmask encoding which of n ≤ ~20 elements are used, plus optionally the last element; transitions add one bit.

O(2^n · n²) for TSP-style (mask, last) states; O(2^n · n) for dp[mask] with one-bit transitions; O(3^n) for submask enumeration · O(2^n · n) or O(2^n) space
Digit DP

Count numbers in [0, N] with a digit property by scanning N's digits with a "tight" flag and a small property state.

O(D × S × B) — D digits (≤ 19), S property states, B base (10) · O(D × S) space
DP on DAGs
▶ viz

State is a vertex; process vertices in topological order so every predecessor is finalized before its successors.

O(V + E) · O(V + E) space
Fibonacci Numbers
▶ viz

Compute F(n) = F(n-1) + F(n-2) in linear time by reusing the two previous values instead of recomputing them.

O(n) · O(1) space
Climbing Stairs
▶ viz

Count the ways to reach step n taking 1 or 2 steps at a time — a Fibonacci recurrence in disguise.

O(n) · O(1) space
0/1 Knapsack
▶ viz

Choose a subset of items, each used at most once, maximizing total value without exceeding a weight capacity.

O(n·W) · O(W) space
Unbounded Knapsack
▶ viz

Maximize value under a capacity when every item may be taken any number of times — the 0/1 loop run forward.

O(n·W) · O(W) space
Coin Change
▶ viz

Find the fewest coins that sum to an amount (or count the ways) using unlimited coins of given denominations.

O(amount · k) · O(amount) space
Longest Increasing Subsequence
▶ viz

Find the length of the longest strictly increasing subsequence — O(n²) DP or O(n log n) with patience sorting.

O(n log n) · O(n) space
Longest Common Subsequence
▶ viz

Find the longest subsequence shared by two sequences using a 2D table over prefix pairs.

O(n·m) · O(min(n, m)) space
Edit Distance
▶ viz

Minimum number of insertions, deletions, and substitutions to turn one string into another via a 2D prefix table.

O(n·m) · O(min(n, m)) space
Matrix Chain Multiplication
▶ viz

Choose the parenthesization of a matrix product that minimizes scalar multiplications — the archetypal interval DP.

O(n³) · O(n²) space
Kadane's Algorithm
▶ viz

Find the maximum-sum contiguous subarray in one pass by tracking the best sum ending at each position.

O(n) · O(1) space
House Robber
▶ viz

Maximize the sum of chosen array elements with no two adjacent — a take-or-skip 1D DP with two rolling variables.

O(n) · O(1) space