leetcode 322. Coin Change

class Solution(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        if amount == 0:
            return 0
        length = amount+1
        dp = [2**32 for i in range(length)]
        dp[0] = 0
        for i in range(1,length,1):
            for coin in coins:
                if coin <= i:
                    dp[i] = min(dp[i],dp[i-coin]+1)
        return dp[amount] if dp[amount]<=amount else -1

你可能感兴趣的:(LeetCode)