Skip to content

Instantly share code, notes, and snippets.

View shangshang-wang's full-sized avatar
🎧
Improvise, Adapt, Overcome

Shangshang Wang shangshang-wang

🎧
Improvise, Adapt, Overcome
View GitHub Profile
@shangshang-wang
shangshang-wang / dp_memoize.py
Created July 13, 2020 08:54
Find Sets Of Numbers That Add Up To Certain Number, original from CS Dojo on Youtube
def count_sets_dp(arr, total):
mem = {}
return dp(arr, total, len(arr)-1, mem)
def dp(arr, total, i, mem):
key = str(total) + ":" + str(i)
if key in mem: return mem[key]
if total == 0: return 1
else if total < 0: return 0
else if i < 0: return 0
@shangshang-wang
shangshang-wang / bottom_up.py
Created July 13, 2020 08:48
Fibonacci problem using dynamic programming, original from CS Dojo on Youtube
def fib_bottom_up(n):
if n == 1 or n == 2:
return 1
bottom_up = [None] * (n+1)
bottom_up[1] = 1
bottom_up[2] = 1
for i in range(3, n+1):
bottom_up[i] = bottom_up[i-1] + bottom_up[i-2]
return bottom_up[n]