Last active
August 29, 2015 14:02
-
-
Save hillscottc/a16e59fbf44329010284 to your computer and use it in GitHub Desktop.
Coin change problem, dynamic.
This file contains 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 make_change(cents_needed, coin_vals): | |
min_coins = [[0 for j in range(cents_needed + 1)] | |
for i in range(len(coin_vals))] | |
min_coins[0] = range(cents_needed + 1) | |
for i in range(1, len(coin_vals)): | |
for j in range(0, cents_needed + 1): | |
if j < coin_vals[i]: | |
min_coins[i][j] = min_coins[i-1][j] | |
else: | |
min_coins[i][j] = min(min_coins[i-1][j], | |
1 + min_coins[i][j - coin_vals[i]]) | |
return min_coins | |
if __name__ == '__main__': | |
min_coins = make_change(30, [1, 10, 25]) | |
for row in min_coins: | |
print row | |
print min_coins[-1][-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment