Now, head to LeetCode and implement the memoized solution yourself. Try writing it from scratch without looking at the code provided. Think through the recurrence relation, identify the base cases, then add memoization. The implementation follows directly from the recurrence. Each line of code corresponds to part of the mathematical formula. Walk through a small example step by step to verify your understanding.
Time complexity: .
Space complexity: linear scan with constant variables.