Memoization
Top-down dynamic programming: cache results of recursive calls.
Intermediate
from functools import cache
@cache
def ways(n): return 1 if n <= 1 else ways(n-1) + ways(n-2)
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.