322. change exchange leetcode

Here Insert Picture Description

Solution to a problem - Dynamic Programming

Here Insert Picture Description

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]

Here Insert Picture Description

Published 284 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_39289876/article/details/104808917