Dynamic Programming problems, grouped by pattern

Dynamic programming is recursion that remembers. Start from the recursive choice, add memoisation, then turn it into a table. The patterns move from 1D and grid DP to stock state machines, knapsack, string alignment and interval DP.

When to reach for it

  • Count the ways, or find the min or max over choices
  • Overlapping subproblems in the recursion tree
  • Two strings compared character by character

Mistakes to watch for

  • Wrong base cases for empty input
  • Iterating capacity in the wrong direction for 0/1 knapsack
  • Defining dp[i] without writing down what it means

The 34 problems

11D DP

2Alternate Selection / Pick / Not pick

32D DP

42D DP - max/min of last row

5Squares

7Knapsack

9Interval DP