Some recursive problems solve the same little subproblem over and over — so the naive version does an astronomical amount of redundant work. The fix is almost embarrassingly simple: remember answers you've already computed. That single idea — caching overlapping subproblems — is the heart of dynamic programming, and it can turn an O(2ⁿ) algorithm into a linear one. Watch a Fibonacci call tree explode, then collapse.
To compute fib(n), the naive definition calls fib(n−1) + fib(n−2) — and each of those splits again, all the way down to fib(1) and fib(0). The trouble: the same subproblems are recomputed many times. fib(2) alone gets recalculated again and again. The total number of calls is exactly 2·fib(n+1) − 1, which grows roughly like O(2ⁿ).
Now keep a cache. The first time we compute fib(k) we store it; every later request for the same k is a cache HIT (flashing green) — we return the saved value instantly and never re-expand that branch. The exponential tree collapses to one unique call per value: about n + 1 real computations. Same answer, dramatically less work.
Dynamic programming applies whenever a problem has both of these properties:
Top-down (memoization) writes the natural recursion and adds a cache, computing subproblems lazily — only the ones you actually need. Bottom-up (tabulation) drops recursion and fills a table in dependency order. Both run in O(n) here; the difference is style and call-stack vs table.
The trade-off is time vs space: the cache costs extra memory, but it buys back an exponential amount of recomputation. (And Fibonacci needs only the last two values, so its table can shrink to O(1) space.)
The same pattern powers many classic problems: