Skip to content

Instantly share code, notes, and snippets.

@samsonq
Created October 26, 2022 14:08
Show Gist options
  • Save samsonq/6a7bb22be84c941a9ee67d6a988e8f61 to your computer and use it in GitHub Desktop.
Save samsonq/6a7bb22be84c941a9ee67d6a988e8f61 to your computer and use it in GitHub Desktop.
Dynamic Programming
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