python算法之找钱问题
"""
给你 k 种面值的硬币,面值分别为 c1, c2 ... ck,每种硬币的数量无限,再给一个总金额 amount,问你最少需要几枚硬币凑出这个金额,如果不可能凑出,算法返回 -1 。算法的函数签名如下:
"""
coins = [1, 2, 5]
money = 11
def coin_change(coin_list, amount):
if amount == 0:
return 0
if amount<0:
return -1
else:
min = coin_change(coin_list, amount-coin_list[0]) + 1
for coin in coin_list[1:]:
res = coin_change(coin_list, amount - coin) + 1
if res < min and res != 0:
min = res
return min
print(coin_change(coins, money))
当值未一旬,而视茫茫,而发苍苍,而齿牙动摇