动态规划——详解leetcode518 零钱兑换 II

Posted qiangz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划——详解leetcode518 零钱兑换 II相关的知识,希望对你有一定的参考价值。

动态规划 零钱兑换 II

参考书目:《程序员代码面试指南:IT名企算法与数据结构题目最优解》

给定不同面额的硬币和一个总金额。写出函数来计算可以凑成总金额的硬币组合数。假设每一种面额的硬币有无限个。

示例 1:

输入: amount = 5, coins = [1, 2, 5]
输出: 4
解释: 有四种方式可以凑成总金额:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
示例 2:

输入: amount = 3, coins = [2]
输出: 0
解释: 只用面额2的硬币不能凑成总金额3。
示例 3:

输入: amount = 10, coins = [10]
输出: 1

注意:

你可以假设:

0 <= amount (总金额) <= 5000
1 <= coin (硬币面额) <= 5000
硬币种类不超过 500 种
结果符合 32 位符号整数

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/coin-change-2
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1. 暴力递归

class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        if amount == 0:
            return 1
        if not coins or amount < 0:
            return 0
        return self.process(coins, 0, amount)
    
    def process(self, coins, index, amount):
        res = 0
        if index == len(coins):
            return 1 if amount == 0 else 0
        else:
            i = 0
            while coins[index] * i <= amount:
                res += self.process(coins, index+1, amount - i*coins[index])
                i += 1
        return res

暴力递归方法的时间复杂度非常高,并且与 arr 中钱的面值有关,最差情况下为O(amountN

2. 记忆化搜索

# 用-1标记是否计算过
class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        if amount == 0:
            return 1
        if not coins or amount < 0:
            return 0
        coin_num = len(coins)
        counts = [[0 for i in range(amount+1)] for j in range(coin_num+1)] 
        return self.process(coins, 0, amount, counts)
    
    def process(self, coins, index, amount, counts):
        res = 0
        if index == len(coins):
            return 1 if amount == 0 else 0
        else:
            i = 0
            while coins[index]*i <= amount:
                value = counts[index+1][amount-coins[index]*i]
                if value != 0:
                    res += 0 if value == -1 else value
                else:
                	res += self.process(coins, index+1, amount-coins[index]*i, counts)
                i += 1
       	counts[index][amount] = -1 if res == 0 else res
    	return res

记忆化搜索方法的时间复杂度为 O(N×amount2)

# 超时代码;未标记是否计算过。
class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        if amount == 0:
            return 1
        if not coins or amount < 0:
            return 0
        coin_num = len(coins)
        counts = [[0 for i in range(amount+1)] for j in range(coin_num+1)] 
        return self.process(coins, 0, amount, counts)
    
    def process(self, coins, index, amount, counts):
        res = 0
        if index == len(coins):
            return 1 if amount == 0 else 0
        else:
            i = 0
            while coins[index]*i <= amount:
                value = counts[index+1][amount-coins[index]*i]
                if value != 0:
                    res += value
                else:
                	res += self.process(coins, index+1, amount-coins[index]*i, counts)
                i += 1
        counts[index][amount] = res
    	return res

3. 动态规划方法

时间复杂度为 O(N×amount2

class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        if amount == 0:
            return 1
        if not coins or amount < 0:
            return 0
        return self.process(coins, amount)
    
    def process(self, coins, amount):
        counts = [[0 for i in range(amount+1)] for j in range(len(coins)+1)]
        for i in range(len(coins)):
            counts[i][0] = 1
        j = 0
        while coins[0]*j <= amount:
            counts[0][coins[0]*j] = 1
            j += 1
        for i in range(1, len(coins)):
            for j in range(1, amount+1):
                num = 0
                k = 0
                while coins[i]*k <= j:
                    num += counts[i-1][j-coins[i]*k] 
                    k += 1
                counts[i][j] = num
        return counts[len(coins)-1][amount]
        

记忆化搜索的方法说白了就是不关心到达某一个递归过程的路径,只是单纯地对计算过的递归过程进行记录,避免重复的递归过程,而动态规划的方法则是规定好每一个递归过程的计算顺序,依次进行计算,后计算的过程严格依赖前面计算过的过程。

4. 进一步优化的动态规划算法

时间复杂度O(N×amount)

class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        if amount == 0:
            return 1
        if not coins or amount < 0:
            return 0
        return self.process(coins, amount)
    
    def process(self, coins, amount):
        counts = [[0 for i in range(amount+1)] for j in range(len(coins)+1)]
        for i in range(len(coins)):
            counts[i][0] = 1
        j = 0
        while coins[0]*j <= amount:
            counts[0][coins[0]*j] = 1
            j += 1
        for i in range(1, len(coins)):
            for j in range(1, amount+1):
                counts[i][j] = counts[i-1][j]
                counts[i][j] += counts[i][j-coins[i]] if j - coins[i] >= 0 else 0 # 简化为dp[i][j]=dp[i-1][j]+dp[i][j-arr[i]]。一下省去了枚举的过程,时间复杂度也减小至O(N×amount)
        return counts[len(coins)-1][amount]

5. 对空间进一步优化

class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int
        """
        if amount == 0:
            return 1
        if not coins or amount < 0:
            return 0
        return self.process(coins, amount)
    
    def process(self, coins, amount):
        counts = [0 for i in range(amount+1)]
        j = 0
        while coins[0]*j <= amount:
            counts[coins[0]*j] = 1
            j += 1
        for i in range(1, len(coins)):
            for j in range(1, amount+1):
                counts[j] += counts[j-coins[i]] if j - coins[i] >= 0 else 0 # 空间压缩
        return counts[amount]

时间复杂度为O(N×aim)、额外空间复杂度O(aim)的方法。

















以上是关于动态规划——详解leetcode518 零钱兑换 II的主要内容,如果未能解决你的问题,请参考以下文章

leetcode经典动态规划题目:518零钱兑换

518. 零钱兑换 II -- LeetCode -- 6.10

leetcode 518. 零钱兑换 II-----完全背包套路模板

动态规划第九篇:零钱兑换

动态规划第九篇:零钱兑换

[LeetCode] 322. 零钱兑换 ☆☆☆(动态规划)