Fibonacci Sequence (DP)
6 min read.
A classic example of Dynamic Programming. We calculate the Nth number by summing the two preceding ones, storing results in a table (Bottom-Up) to avoid redundant calculations.
Think of it this way: Think of climbing a staircase. To step onto step N, you must have come from either step N-1 or step N-2. The number of ways to reach the top is the sum of the ways to reach the previous two steps.
Recursion with a memo for fib(6).
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Three ways to compute fib(n)
Sort each trait into the approach it describes.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
Naive recursion
Drop items hereMemoization (top-down)
Drop items hereTabulation (bottom-up)
Drop items hereQuiz
20 questionsWhy is DP faster than simple recursion for Fibonacci?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.