The four questions

  • What's the state? (dp[i], dp[i][j])
  • What's the transition? (dp[i] = f(dp[i-1]))
  • What's the base case?
  • What's the order of filling?
Advertisement

Top-down vs bottom-up

Top-down (memoization): recurse + cache. Bottom-up (tabulation): iterate + fill array. Same complexity. Bottom-up avoids stack overflow.

Advertisement

The four questions

  • What's the state? (dp[i], dp[i][j])
  • What's the transition? (dp[i] = f(dp[i-1]))
  • What's the base case?
  • What's the order of filling?

Top-down vs bottom-up

Top-down (memoization): recurse + cache. Bottom-up (tabulation): iterate + fill array. Same complexity. Bottom-up avoids stack overflow.

Recognizing DP problems

Optimal substructure (subproblems compose) + overlapping subproblems (same subproblem seen many times). Both required.

Complexity analysis

// States = O(N)
// Transition = O(1)
// Total time = O(N)
// Fibonacci: 2^N recursion → N with DP