Created
May 11, 2020 08:51
-
-
Save sharathsamala/57beaba3d4b58de42ff8c9926e2dfec3 to your computer and use it in GitHub Desktop.
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 dp_min_coins(target, coins, cache): | |
# base case | |
min_coins = target | |
if target in coins: | |
cache[target] = 1 | |
return 1 | |
elif cache[target] > 0: | |
return cache[target] | |
else: | |
for i in [c for c in coins if c <= target]: | |
num_coins = 1 + dp_min_coins(target-i, coins, cache) | |
if num_coins < min_coins: | |
min_coins = num_coins | |
cache[target] = min_coins | |
return min_coins | |
target = 74 | |
coins = (1,5,10,25) | |
cache = [0]*(target+1) | |
print(dp_min_coins(target,coins,cache)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment