

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Intro to Dynamic Programming
incomplete
2: Fast Fibonacci – Memoization
incomplete
3: Memoization
incomplete
4: Fast Fibonacci – Tabulation
incomplete
5: Fast Fibonacci – Review
incomplete
This lesson's interactive features are locked, please to keep using them
You've almost certainly noticed that the memoized and tabulated versions of the Fibonacci algorithm are both faster than the naïve recursive version.
def fibonacci(n: int) -> int:
fib_arr: list[int] = [0] * (n + 1)
fib_arr[0] = 0
if n > 0:
fib_arr[1] = 1
for i in range(2, n + 1):
fib_arr[i] = fib_arr[i - 1] + fib_arr[i - 2]
return fib_arr[n]
def fibonacci(n: int) -> int:
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)