Here's the complete memoized Fibonacci:
function fib(n, dp)
if dp[n] ≠ -1 then
return dp[n]
if n ≤ 1 then
return n
dp[n] := fib(n - 1, dp) + fib(n - 2, dp)
return dp[n]
You added just three lines: the lookup check, the storage line, and passing dp through recursive calls. The rest is identical to your original recursive solution. Three extra lines. Before: if dinosaurs had started computing fib(), we would still be waiting for the answer. After: finishes in the time it takes to blink. This same pattern appears in game AI, route planning, stock trading. You will use it for years.