Explain the Fibonacci sequence using DP.
Updated Apr 28, 2026
Short answer
The Fibonacci sequence can be solved using Dynamic Programming by storing previously calculated Fibonacci numbers instead of recomputing them. The DP state is usually defined as dp[i], where dp[i] represents the i-th Fibonacci number. This reduces the time complexity from the naive recursive approach's O(2^n) to O(n).
Deep explanation
The Fibonacci sequence follows this mathematical rule:
F(0) = 0F(1) = 1
F(n) = F(n - 1) + F(n - 2)A naive recursive solution directly follows this formula:
def fibonacci(n): if n <= 1: return n
return fibonacci(n - 1) + fibonacci(n - 2)The problem is that the same values are calculated many times.
For example:
fibonacci(5)
needs:fibonacci(4)fibonacci(3)
fibonacci(4) needs:fibonacci(3)fibonacci(2)The value of fibonacci(3) is calculated repeatedly.
Dynamic Programming solves this by storing results after calculating them once.
DP State Definition
The first step is defining the state:
dp[i] = the i-th Fibonacci numberEach index stores a solved subproblem:
dp[0] = 0dp[1] = 1dp[2] = 1dp[3] = 2dp[4] = 3The transition is:
dp[i] = dp[i - 1] + dp[i - 2]Bottom-Up DP (Tabulation)
The bottom-up approach starts from the base cases and builds the answer step by step.
Implementation:
def fibonacci(n): if n <= 1: return n
dp = [0] * (n + 1)
dp[0] = 0 dp[1] = 1
for i in range(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]Example:
print(fibonacci(7))The DP table is built as:
Index: 0 1 2 3 4 5 6 7Value: 0 1 1 2 3 5 8 13The answer is:
F(7) = 13Complexity Analysis
For the full DP table solution:
Time Complexity: O(n)Space Complexity: O(n)Why?
- The loop calculates each Fibonacci number exactly once.
- The array stores
n + 1Fibonacci values.
Space Optimized DP
Notice that to calculate the next Fibonacci number, we only need the previous two values:
F(n) = F(n - 1) + F(n - 2)We do not need the entire DP array.
Optimized implementation:
def fibonacci(n): if n <= 1: return n
previous = 0 current = 1
for _ in range(2, n + 1): previous, current = current, previous + current
return currentComplexity:
Time Complexity: O(n)Space Complexity: O(1)Top-Down DP (Memoization)
Another approach is to keep the recursive structure but store already computed results.
def fibonacci(n, memo={}): if n in memo: return memo[n]
if n <= 1: return n
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
return memo[n]The memo prevents repeated calculations.
Complexity:
Time Complexity: O(n)Space Complexity: O(n)Why Fibonacci Is a DP Problem
Fibonacci has two important properties that make it suitable for Dynamic Programming:
- Overlapping subproblems
The same smaller Fibonacci values are needed multiple times.
Example:
``text id="y6q2rx" fibonacci(5) and fibonacci(4) both require fibonacci(3) ``
- Optimal substructure
The solution to a larger problem can be built from solutions to smaller problems.
Example:
``text id="u3m8kw" F(5) = F(4) + F(3) ``
Real-world example
Suppose a system needs to generate Fibonacci values for a mathematical visualization tool.
Using DP:
def generate_fibonacci(n): if n <= 0: return []
if n == 1: return [0]
dp = [0, 1]
for i in range(2, n): dp.append(dp[i - 1] + dp[i - 2])
return dp
print(generate_fibonacci(10))Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]The program calculates each value once and reuses previously computed results.
Common mistakes
- * Implementing naive recursion and calling it a DP solution.
- * Forgetting to define what `dp[i]` represents.
- * Initializing incorrect base cases such as `dp[0] = 1`.
- * Using a DP array when only two previous values are needed.
- * Confusing memoization with tabulation.
- * Forgetting that DP works by storing results of overlapping subproblems.
- * Ignoring edge cases like `n = 0` and `n = 1`.
Follow-up questions
- Why does Dynamic Programming improve Fibonacci performance?
- What is the DP state for Fibonacci?
- What is the difference between memoization and tabulation for Fibonacci?
- Why can Fibonacci DP use O(1) space?
- Does Fibonacci always require Dynamic Programming?
- What are the two properties that make Fibonacci suitable for DP?