Patterns

DP · Pattern 17

Dynamic Programming

Break a problem into overlapping subproblems. Memoize results (top-down) or fill a table bottom-up. Works when the problem has optimal substructure.

1111123413610141020i=0i=1i=2i=3
Time
O(n²) or O(n·m) typically
Space
O(n) to O(n·m)

Spot it when

  • Count ways / find min/max ways to reach a goal
  • Optimal substructure: solution built from subproblem solutions
  • "How many ways...?" or "What is the minimum cost...?"
  • Substring/subsequence problems (LCS, LIS)