What is the Fibonacci Sequence DP problem?
The Fibonacci sequence is the classic introduction to Dynamic Programming. Each number is the sum of the two preceding ones: F(0) = 0, F(1) = 1, and F(n) = F(n-1) + F(n-2) for n > 1. A naive recursive solution recomputes the same subproblems repeatedly, leading to exponential time complexity O(2^n).
By storing each computed value in a table (tabulation) instead of recalculating it, we only ever solve each subproblem once. This is the core idea behind Dynamic Programming: trade memory for time by caching overlapping subproblems, similar to the approach used in the Knapsack Problem and Longest Common Subsequence.
Time & Space Complexity
- Naive RecursionO(2^n)
- DP Tabulation TimeO(n)
- DP Tabulation SpaceO(n)
- Optimized SpaceO(1)
* Space can be reduced to O(1) by only keeping the last two values instead of the full table.
Why use Dynamic Programming?
- ✓Overlapping SubproblemsF(5) and F(4) both depend on F(3), so recomputing it repeatedly wastes work.
- ✓Optimal SubstructureThe solution to F(n) is built directly from the solutions to smaller subproblems.
- ✓Foundational PatternThe same tabulation technique scales up to far more complex problems like Knapsack and LCS.