Dynamic programming
Solve a collection of overlapping subproblems once each, reusing their answers in a dependency order. Memoization is top-down; tabulation is bottom-up.
REFERENCE SHEET
Identify what information a subproblem needs, or prove why a local choice is safe.
Before this Asymptotics & recurrences, Recursion, invariants & termination
Solve a collection of overlapping subproblems once each, reusing their answers in a dependency order. Memoization is top-down; tabulation is bottom-up.
An optimal solution can be assembled from optimal solutions of the correctly specified subproblems.
Commit to a local choice without revisiting it. A correctness proof must show that some optimum is compatible with each choice.
For unweighted intervals, repeatedly choosing the compatible interval with earliest finish time maximizes the number chosen. An exchange argument replaces the first interval of an optimum without reducing its remaining options.
For positive integer denominations and nonnegative target A, the displayed recurrence gives the minimum number of coins, with unreachable states +∞. Evaluate amounts increasingly in O(Ak) time for k denominations.
Edit distance uses prefix-pair states to compare strings.
Knapsack and scheduling problems expose how state design captures remaining choices and constraints.
01 / Find a counterexample
Compare starting with 4 to using 3 twice.
Greedy returns 4+1+1, using 3 coins. The optimum is 3+3, using 2, so greedy fails.
02 / Compute
Use smaller amounts and the displayed recurrence.
The values are 0,1,2,1,1,2,2. For amount 5 use 4+1; for 6 use 3+3.
Further reading and full-length treatments: