← Dynamic Programming

Pattern Recognition Drill

#47 — Fibonacci (memoized)

Easy Recursion

The Problem

Compute the nth Fibonacci number efficiently.

What approach would you use?

Think about it before scrolling down.

Key Signals

Dynamic Programming

Memoize or tabulate. dp[i] = dp[i-1] + dp[i-2]. O(n) time, O(n) or O(1) space.

Alt: DFS / Recursion

Naive recursion is O(2^n) — exponential. Adding @lru_cache makes it O(n).

Common Trap

The naive recursive tree is the textbook example of why memoization matters.

← #46 #48 →