Skip to content

Instantly share code, notes, and snippets.

@shage001
Created February 4, 2019 19:00
Show Gist options
  • Save shage001/a3bbb5845d03b72f1e9469e85c91bd22 to your computer and use it in GitHub Desktop.
Save shage001/a3bbb5845d03b72f1e9469e85c91bd22 to your computer and use it in GitHub Desktop.
def make_change(coins, target):
min_coins = [0 for i in range(0, target + 1)]
sets = {i:[] for i in range(target + 1)}
for i in range(1, target + 1):
smallest = float("inf")
for coin in coins:
if (coin <= i):
smallest = min(smallest, min_coins[i - coin])
if smallest == min_coins[i - coin]:
sets[i] = [coin] + sets[i - coin]
min_coins[i] = 1 + smallest
return min_coins[target], sets[target]
print make_change([1, 5, 10, 25, 100], 165) # => (5, [100, 25, 25, 10, 5])
print make_change([1, 3, 4], 6) # => (2, [3, 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment