322. 零钱兑换leetcode

在这里插入图片描述

题解 -动态规划

在这里插入图片描述

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        dp=[float('inf')]*(amount+1)
        dp[0]=0
        for coin in coins:
            for x in range(coin,amount+1):
                dp[x]=min(dp[x],dp[x-coin]+1)
        if dp[amount]==float('inf'):
            return -1
        else:
            return dp[amount]

在这里插入图片描述

发布了284 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_39289876/article/details/104808917