Leetcode 322 零钱兑换

2020-06-27  本文已影响0人  SunnyQjm

零钱兑换

题目

给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1

解答

测试验证

class Solution:
    def coinChange(self, coins, amount):
        """
        :type coins: List[int],
        :type amount: int
        :rtype int

        (knowledge)

        思路:
        1. 使用动态规划;
        2. 定义f(n)为状态转移方程,表示凑n元所需的最少硬币数:
            -1 n < 0
            0  n == 0
            min{f(n - c) + 1 | c ∈ coins}
        """

        dp = [amount + 1 for i in range(amount + 1)]

        dp[0] = 0

        for i in range(len(dp)):
            for coin in coins:
                if i - coin < 0:
                    continue
                dp[i] = min(dp[i], dp[i - coin] + 1)
        return dp[amount] if dp[amount] != amount + 1 else -1


if __name__ == '__main__':
    solution = Solution()
    print(solution.coinChange([1, 2, 5], 11), "= 3")
    print(solution.coinChange([2], 3), "= -1")
上一篇 下一篇

猜你喜欢

热点阅读