322.零钱兑换
class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
n = len(coins)
dp = [float('inf')]*(amount + 1) # 初始值为正无穷大
dp[0] = 0 # 一定要初始化为0
if amount == 0:
return 0
for num in coins:
for j in range(num,amount+1):
if dp[j-num] !=float('inf'):
dp[j] = min(dp[j],dp[j-num]+1)
if dp[amount] == float('inf'):
return -1
return dp[-1]
题目链接:322. 零钱兑换 - 力扣(LeetCode)