This file contains hidden or 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 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 |
This file contains hidden or 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_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] |