Skip to content

Instantly share code, notes, and snippets.

@hillscottc
Last active August 29, 2015 14:02
Show Gist options
  • Save hillscottc/a16e59fbf44329010284 to your computer and use it in GitHub Desktop.
Save hillscottc/a16e59fbf44329010284 to your computer and use it in GitHub Desktop.
Coin change problem, dynamic.
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