BeginnerAbout 10 min

    Fibonacci Sequence (DP)

    Read the full article on Substack

    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.

    memo0123456

    Recursion with a memo for fib(6).

    1/18

    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

    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 here

    Memoization (top-down)

    Drop items here

    Tabulation (bottom-up)

    Drop items here
    6 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    Why is DP faster than simple recursion for Fibonacci?

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going