🏠
Guest Not signed in

Dynamic programming: memoize the overlapping subproblems

When the optimal answer at size $n$ depends on optimal answers at sizes $<n$, memoize the subproblems and you're done.

Method · Dynamic Programming
Prereqs: Recursion
Intro
Animated 0/1 knapsack DP table being filled cell by cell β€” the canonical DP-table visualization.
User:RDSEED, CC BY-SA 4.0 · CC-BY-SA-4.0 · Wikimedia Commons

Dynamic programming is recursion plus a cache. The trigger is overlap: when the naive recursion would recompute the same subproblem $f(k)$ many times across different branches, you store each answer the first time and reuse it forever after. That’s it β€” find the recurrence, confirm subproblems repeat, fill a table bottom-up. The same two-step move handles climbing-stairs, grid paths, coin change, edit distance, knapsack, and most of the DP-tagged FAANG interview canon.

βœ“ Intro Β· expand
Independent · Legal