Concept
Memoisation caches the result of a function for each input, so a recursion that would recompute the same subproblems many times computes each once. It turns exponential time into polynomial time.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Memoisation doing its job.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
fib(80) # instant; without the cache it never finishes
def climb(n, memo={}): # the hand-rolled version
if n <= 1: return 1
if n not in memo:
memo[n] = climb(n - 1) + climb(n - 2)
return memo[n]Memoising a function whose arguments include something irrelevant to the result, such as an index and a copy of the whole list. The cache key never repeats and nothing is saved.
Your roadmap puts these before Memoisation, unless you already know them.
Once a session shows you understand Memoisation, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Memoisation.