DSA Notes
Master Fibonacci and Fibonacci-like problems with dynamic programming — naive recursion O(2^n), memoization O(n), tabulation O(n), space-optimized O(1) space, matrix exponentiation O(log n), and classic DP problems that reduce to Fibonacci variants like climbing stairs, house robber, and tile tiling.
Why Fibonacci Is the Perfect First DP Problem
The Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13, 21, ...) is the gateway to dynamic programming. It perfectly demonstrates:
- Why naive recursion is exponential
- How memoization reduces it to linear
- How tabulation eliminates recursion
- How space optimization reduces memory
- How matrix exponentiation achieves O(log n)
More importantly, dozens of real DP problems are fundamentally Fibonacci variants — once you understand Fibonacci deeply, you recognize them instantly.
Level 2 — Memoization: O(n) time, O(n) space
from functools import cache
@cache
def fib_memo(n):
if n <= 1: return n
return fib_memo(n-1) + fib_memo(n-2)
print(fib_memo(100)) # 354224848179261915075 — instantEach fib(i) computed exactly once → n total computations.
Level 3 — Tabulation: O(n) time, O(n) space
Level 4 — Space Optimized: O(n) time, O(1) space
def fib_optimal(n):
"""Only need the last two values, not the full array."""
if n <= 1: return n
prev2, prev1 = 0, 1
for _ in range(2, n+1):
prev2, prev1 = prev1, prev2 + prev1
return prev1
print(fib_optimal(100)) # 354224848179261915075This is the production solution for all Fibonacci-like 1D DP problems.
Level 5 — Matrix Exponentiation: O(log n)
For astronomically large n (n = 10^18), even O(n) is too slow. Matrix exponentiation computes fib(n) in O(log n):
Fibonacci Variants — The Real Interview Problems
Problem 1 — Climbing Stairs
"Count ways to climb n stairs, taking 1 or 2 steps at a time."
Problem 2 — Minimum Cost Climbing Stairs
Problem 3 — House Robber
"Rob houses, but cannot rob two adjacent houses. Maximize total."
Problem 4 — House Robber II (Circular)
Problem 5 — Tile Tiling (Domino Tiling)
"How many ways to tile a 2×n board with 1×2 dominoes?"
Problem 6 — k-Steps Climbing Stairs (Tribonacci/Generalized)
The Fibonacci Pattern Recognition
Any DP problem of the form:
is a Fibonacci variant. Recognize it by:
- State only depends on a FIXED number of previous states
- No 2D table needed
- Space-optimized solution uses a few variables, not an array
| Fibonacci | dp[i] = dp[i-1] + dp[i-2] ← 2 previous |
| Tribonacci | dp[i] = dp[i-1] + dp[i-2] + dp[i-3] ← 3 previous |
| House Robber | dp[i] = max(dp[i-1], dp[i-2] + nums[i]) ← 2 previous |
All solved in O(n) time and O(1) space.
Summary
| Approach | Time | Space | Use When |
|---|---|---|---|
| Naive recursion | O(2^n) | O(n) | Never in production |
| Memoization | O(n) | O(n) | Quick implementation |
| Tabulation | O(n) | O(n) | Clear table structure |
| Space-optimized | O(n) | O(1) | Production code |
| Matrix exp. | O(log n) | O(1) | n up to 10^18 |
The Fibonacci problem is a microcosm of all 1D DP: recognize the recurrence, implement with rolling variables, achieve O(1) space.
*Next Lesson: Knapsack Problem*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Fibonacci DP — From Exponential to Linear with Memoization and Matrix Exponentiation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, dynamic, programming, fibonacci
Related Data Structures & Algorithms Topics