Common Mistakes

Common errors to avoid

  • Wrong base case initialization: dp[0] is often 0, 1, or true depending on problem semantics. Think carefully about what empty input means.
  • Off-by-one in array indexing: Whether dp[i] represents "up to index i" or "exactly at index i" affects loop bounds and transitions.
  • Forgetting impossible states: In minimization problems, initialize with infinity.

Check for impossible results before returning.

  • Not recognizing space optimization opportunity: Many 1D DP problems only need previous 11-22 values, reducing O(n)O(n) space to O(1)O(1).