Skip to content

Instantly share code, notes, and snippets.

@rongpenl
Last active March 22, 2024 17:14
Show Gist options
  • Save rongpenl/33d69b28c79aaf39b222a718d6fa5eff to your computer and use it in GitHub Desktop.
Save rongpenl/33d69b28c79aaf39b222a718d6fa5eff to your computer and use it in GitHub Desktop.
Answer for the mail delivery problem using dynamic programming
# ron and math community post: https://www.youtube.com/post/Ugkx321bNyuBEYuPthOIEKpbaqz04Ujat1f_
def a(n):
if n < 3:
return 1
if n not in memo_a:
memo_a[n] = a(n - 2) + a(n - 3)
return memo_a[n]
def b(n):
if n not in memo_b:
memo_b[n] = a(n - 1) + a(n - 2)
return memo_b[n]
memo_a = {}
memo_b = {}
print(a(19) + b(19)) # 351
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment