Created
October 26, 2022 14:08
-
-
Save samsonq/6a7bb22be84c941a9ee67d6a988e8f61 to your computer and use it in GitHub Desktop.
Dynamic Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib_dp(n, memo): | |
if n in memo: | |
return memo[n] | |
else: | |
fib_n = fib_dp(n-1, memo) + fib_dp(n-2, memo) | |
memo[n] = fib_n | |
return fib_n | |
def fibonacci_dp(n): | |
return fib_dp(n, {1: 1, 2: 1}) | |
print(fibonacci_dp(9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment