class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
n = len(coins)
# dp[i]表示amount=i需要的最少coin数
dp = [float("inf")] * (amount+1)
dp[0] = 0
for i in range(amount+1):
for j in range(n):
# 只有当硬币面额不大于要求面额数时,才能取该硬币
if coins[j] <= i:
dp[i] = min(dp[i], dp[i-coins[j]]+1)
# 硬币数不会超过要求总面额数,如果超过,说明没有方案可凑到目标值
return dp[amount] if dp[amount] <= amount else -1
来源:CSDN
作者:ailinyingai
链接:https://blog.csdn.net/ailinyingai/article/details/104030940