Codacoda
Back to Academy

dynamic programming

Fibonacci

The Fibonacci sequence is a classic dynamic programming problem where each number is the sum of the two preceding ones. A naive recursive approach has exponential time complexity, but by storing previously computed values in a DP table (memoization or bottom-up tabulation), we reduce it to linear time. This illustrates the core DP principle of trading space for time by eliminating redundant subproblem calculations.

Use Cases

  • Modeling growth patterns in nature (population growth, spiral structures)
  • Financial mathematics for option pricing and interest calculations
  • Teaching foundational dynamic programming and memoization techniques

Complexity Analysis

MetricBestAverageWorst
TimeO(n)O(n)O(n)
SpaceO(n)

Visualization

F(0)F(1)F(2)F(3)F(4)F(5)F(6)F(7)F(8)F(9)F(10)
0
1
0
0
0
0
0
0
0
0
0
Speed:1x
Initialize: F(0)=0, F(1)=1Step 1 / 11

Implementation

Output

Click "Run Code" to see output...