1-D DP
Recipe
- State:
dp[i]= answer for the prefix ending at / of lengthi. - Transition: how
dp[i]depends on smaller states (try every "last decision"). - Base case and answer location.
- 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
| Pattern | Examples |
|---|---|
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 max | 152, 300, 53 |
| Expand around centre / interval | 5, 647 |
Problems
Showing 29 of 29