Data Structures & Algorithms · Revision notes

Dynamic Programming (DP)

Dynamic programming solves problems with optimal substructure and overlapping subproblems by storing results: top-down memoization or bottom-up tabulation. It converts exponential recursion into polynomial time.

Quick revision

  • DP = optimal substructure + overlapping subproblems; time = states x transitions
  • Memo (top-down, lazy) vs table (bottom-up, space-optimizable)
  • Knapsack: dp[c]=max(dp[c], dp[c-w]+v) with c descending; O(nW) pseudo-polynomial
  • LCS: match -> diag+1 else max(up,left); edit distance = 1 + min(3)
  • LIS: O(n^2) dp or O(n log n) tails + bisect_left
Read the full Dynamic Programming (DP) notes on Doubtclip →