Worked Examples: Dynamic Programming I

These examples are designed to step through the logical process of applying the algorithmic concepts.

Example 1: Fibonacci
❓ Problem: Naive vs DP complexity.
💡 Solution: Naive $O(2^n)$. DP $O(n)$.
Example 2: Rod Cutting Recursive
❓ Problem: Recurrence?
💡 Solution: $r_n = \max(p_i + r_{n-i})$.
Example 3: Memoization
❓ Problem: What do we store?
💡 Solution: Max price for length $i$ in array $R[i]$.
Example 4: Reconstruction
❓ Problem: How to find the cuts?
💡 Solution: Store choice $S[i]$ (first cut). Backtrack.
Example 5: Subproblems
❓ Problem: Count for Rod Cutting $n$?
💡 Solution: $n+1$ subproblems.
Example 6: Space
❓ Problem: Space complexity?
💡 Solution: $O(n)$ for 1D table.