SDE & MLE Prep

1-D DP

Recipe

  1. State: dp[i] = answer for the prefix ending at / of length i.
  2. Transition: how dp[i] depends on smaller states (try every "last decision").
  3. Base case and answer location.
  4. Optimise memory when dp[i] only needs the last few states.

Start with top-down recursion + @cache if the recurrence is clearer; convert to bottom-up when needed.

Climbing stairs  dp[i] = dp[i-1] + dp[i-2]
i     0  1  2  3  4  5
dp    1  1  2  3  5  8
PatternExamples
Fibonacci-like (i-1, i-2)70, 746, 198, 213, 91
Unbounded knapsack (dp[a] = min/sum over items)322, 279, 377, 518
0/1 knapsack (iterate capacity downward)416, 494
"Best ending at i" + global max152, 300, 53
Expand around centre / interval5, 647

Problems