322. 零钱兑换

参考解答:动态规划套路详解
思路1:递归。会超时。
时间复杂度为 O(kn)
空间复杂度O(n)

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        def helper(coins, amount): # 可以合并内部函数和本身
            if amount == 0: return 0
            ans = 2 ** 31
            for c in coins:
                newAmount = amount - c
                if newAmount < 0: continue
                subPro = helper(coins, newAmount) # 获得newAmount的最小硬币数
                if subPro == -1: continue
                ans = min(ans, subPro + 1) #  获得amount(=newAmount+c)的最小硬币数
            return ans if ans != 2 ** 31 else -1
        return helper(coins, amount)

思路2:带记忆的递归。有时会超时。
时间复杂度为 O(k*n)
空间复杂度O(n)

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        memo = {}  # 记录不同目标值对应的最小硬币数
        def helper(coins, amount):
            if amount == 0: return 0
            if amount in memo.keys(): return memo[amount]
            ans = 2 ** 31
            for c in coins:
                newAmount = amount - c
                if newAmount < 0: continue
                subPro = helper(coins, newAmount)
                if subPro == -1: continue
                ans = min(ans, subPro + 1)
            memo[amount] = ans if ans != 2 ** 31 else -1
            return memo[amount]
        return helper(coins, amount)

思路3:动态规划。
时间复杂度为 O(k*n)
空间复杂度O(n)

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        dp = [2**31 for _ in range(amount+1)] # 记录每个目标值的最小硬币数,初始化为最大
        dp[0] = 0
        for i in range(1,amount+1):
            for c in coins:
                newAmount = i-c
                if newAmount>=0:
                    dp[i] = min(dp[i],dp[newAmount]+1)  # dp[i]有很多,只保留最小的
        return dp[-1] if dp[-1]!=2**31 else -1 # 若未修改则输出-1

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值