The Fibonacci numbers: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2). Given n, return F(n).
Instead of recursive calls, we build the sequence iteratively. We only need the last two values (a, b) to compute the next, so no memoization array is needed — O(1) space.
The direct recursion calls F of n −1 and F of n minus 2, and each of those calls two more. The same values are computed again and again, so the time grows like 2 to the power n. For example, F of 3 alone is computed three times while finding F of 6.
This is the core idea of dynamic programming: solve each small problem once and store the answer. Build the values from the bottom up, starting with F of 0 and F of 1. Each new value only needs the previous two. So instead of a whole array, two variables are enough.
Overlapping subproblems are the signal for dynamic programming. Keep only the states the next step needs.
Run the animation above on your own input, or watch the 1:47 video explanation of Fibonacci Number with a dry run and Java code.