Memoization
Storing each subproblem's result the first time it is computed, so it is read later instead of computed again.
Example
Climbing 40 stairs 1 or 2 at a time without storing anything makes hundreds of millions of calls; storing each dp[i], it takes 40.
Test yourself: Memoization
Dynamic programming
Storing each result in mem, the time goes from O(2ⁿ) to O(___).
Each of the n subproblems is solved once: with n = 40, about 40 additions.
Solving with DP
In brute force, if an index goes negative, ___ is returned to mark an impossible path.
That way min drops it on its own: min(infinity, 4) = 4.
0/2
▶ Play