LeetCode题解(0322):零钱兑换(Python)

题目:原题链接(中等)

标签:回溯算法、动态规划、记忆化递归

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( C × A ) O(C×A) O(C×A) O ( A ) O(A) O(A)1116ms (87.81%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        dp = [float("inf")] * (amount + 1)
        dp[0] = 0
        for coin in coins:
            for i in range(coin, amount + 1):
                dp[i] = min(dp[i], dp[i - coin] + 1)
        return dp[amount] if dp[amount] != float("inf") else -1
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页