前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【DP】518. Coin Change 2

【DP】518. Coin Change 2

作者头像
echobingo
发布2019-06-14 10:31:42
4330
发布2019-06-14 10:31:42
举报
文章被收录于专栏:Bingo的深度学习杂货店
问题描述:

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

Example 1:
代码语言:javascript
复制
Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:
代码语言:javascript
复制
Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.
Example 3:
代码语言:javascript
复制
Input: amount = 10, coins = [10] 
Output: 1

Note:

You can assume that:

  • 0 <= amount <= 5000
  • 1 <= coin <= 5000
  • the number of coins is less than 500
  • the answer is guaranteed to fit into signed 32-bit integer
解题思路:

这道题是找组合方案数,而不是排列数。因此,如果使用 【DP、BFS】322. Coin Change 这道题中的 BFS 方法,貌似不可行。

但是,可以使用动态规划的思想去解决。创建一个大小为 dp1+amount 的数组,其中 dpj 表示总额为 j 的组合方案数。最后,dp-1 就是答案。

关键是如何找到状态转移方程。因为是要找组合方案数,所以对于 (1 2)、(2 1) 这种的其实是一样的,所以我们的外层循环应该是 coin,即 for coin in coins**(这样就避免出现排列问题)。**那么,内层循环 j 就是用 当前的 coin 去更新这个 dp 数组中的方案数。

转移方程式可以想到:dpj = dp[j-coinsi],但是因为外层循环的每个 coinsi 都可能更新这个 dpj,因此最终的转移方程式应该为:

dp[j] = ∑ dp[j-coins[i]]

表示总额为 j 时的方案数 = 总额为 j-coinsi 的方案数的加和。∑ 次数等于外层循环次数。

记得初始化 dp0 = 1,表示总额为 0 时方案数为 1。其他位置的 dpj 初始化为 0。

时间复杂度 O(c*n),空间复杂度 O(n),其中 c 为硬币种类数,n 为总额 amount。

Python3 实现:
代码语言:javascript
复制
class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        dp = [1] + [0] * amount
        for coin in coins:
            for j in range(coin, amount+1):
                dp[j] += dp[j-coin]  # dp[j] 是所有 coins 累加的结果
        return dp[-1]

print(Solution().change([2,1], 3))  # 2 (111,12)
拓展延伸:

如果这道题是求不同的排列数呢?比如 (1 2)和 (2 1)是不同的情况。那么该怎么去做?

其实,这个问题是 Leetcode 的另一道题:

【377】给一个数组和一个目标,求合成目标的不同组合数,与顺序有关

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题描述:
    • Example 1:
      • Example 2:
        • Example 3:
        • 解题思路:
        • Python3 实现:
        • 拓展延伸:
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档