01 · Question
For a nonnegative integer n, return F(n), where F(0)=0, F(1)=1, and F(n)=F(n−1)+F(n−2) for n>1.
Examples: n=0 → 0; n=1 → 1; n=4 → 3; n=10 → 55. Go results must fit int.
02 · Analysis
Naive recursion: the same states are recomputed many times. O(2^n) time is a valid upper bound (the tighter growth is Θ(φ^n)); the active stack is only O(n). Use it to understand the recurrence, not for large inputs.
Memoization: each state from 0 to n is computed once, giving O(n) time and O(n) cache plus stack space. The cache belongs to this invocation. A zero result must still count as a cache hit; test key presence, not truthiness.
Iteration: evaluate states in increasing order while retaining only the preceding two. O(n) time and O(1) auxiliary space. These complexities count arithmetic operations on fixed-width values. Memoization still has linear depth and does not remove recursion-limit concerns.
03 · Solution