[LeetCode] 518. Coin Change 2 硬币找零 2
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.
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
Example 1:
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:
Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.
Example 3:
Input: amount = 10, coins = [10]
Output: 1
给定一些不同面值的硬币,和一个钱数。编写函数计算要得到目标金额有多少种不同的硬币组合方式。
322. Coin Change的变形,322题是求最少能用几个硬币组成给的钱数,而这题求的是组成给定钱数总共有多少种不同的方法。
解法:动态规划DP, 建立dp数组,保存能到达当前amount的步数。逐个金额遍历,看只用前i个金额能到达j的步数有多少,实现方法是累加起来dp[当前amount - 第i个金额],最后返回dp[amount]。
State: dp[i], 表示总额为i时的方案数
Function: dp[i] = Σdp[i - coins[j]], 表示总额为i时的方案数 = 总额为i-coins[j]的方案数的加和
Initialize: dp[0] = 1, 表示总额为0时方案数为1
Retrun: dp[n] or dp[-1]
Java:
public class Solution {
public int change(int amount, int[] coins) {
if (coins == null || coins.length == 0) {
return amount == 0? 1: 0;
}
int[] dp = new int[amount + 1];
dp[0] = 1;
for (int i = 0; i < coins.length; i ++) {
for (int j = 1; j <= amount; j ++) {
if (j >= coins[i]) {
dp[j] += dp[j - coins[i]];
}
}
}
return dp[amount];
}
}
Python:
class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
dp = [0] * (amount + 1)
dp[0] = 1
for c in coins:
for x in range(c, amount + 1):
dp[x] += dp[x - c]
return dp[amount]
扩展思考:将上述代码中的循环顺序对调,即为求不同硬币的排列数(Permutation)
class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
dp = [0] * (amount + 1)
dp[0] = 1
for x in range(amount + 1):
for c in coins:
if c > x: continue
dp[x] += dp[x - c]
return dp[amount]
C++:
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1, 0);
dp[0] = 1;
for (int coin : coins) {
for (int i = coin; i <= amount; ++i) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
};
类似题目:
[LeetCode] 322. Coin Change 硬币找零
[CareerCup] 9.8 Represent N Cents 组成N分钱
All LeetCode Questions List 题目汇总
[LeetCode] 518. Coin Change 2 硬币找零 2的更多相关文章
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- dp算法之硬币找零问题
题目:硬币找零 题目介绍:现在有面值1.3.5元三种硬币无限个,问组成n元的硬币的最小数目? 分析:现在假设n=10,画出状态分布图: 硬币编号 硬币面值p 1 1 2 3 3 5 编号i/n总数j ...
- codevs 3961 硬币找零【完全背包DP/记忆化搜索】
题目描述 Description 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从银行拿回最少的硬币数,并保证能用这些硬币发工资. 我们应该 ...
- NYOJ 995 硬币找零
硬币找零 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从 ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] Lemonade Change 买柠檬找零
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
随机推荐
- Appium启动淘宝APP,输入搜索内容
# -*- coding:utf-8 -*- from appium import webdriver from time import sleep desired_caps ={ 'platform ...
- VMware下安装的CentOS7.5,设置成静态IP后ping不通外网
网上很多都说用下面的方法即可解决 在CentOS中 ping www.baidu.com 无法ping通,可能原因是DNS没配置好 方法一: 修改vim /etc/resolv.conf 增加如下内容 ...
- C++学习 ---- 系列文章
C++ 第十二课 其它标准C函数 C++ 第十一课 标准c内存函数 C++ 第十课:标准c时间与日期函数 C++ 第九课 标准c数学函数 C++ 第八课 标准c字符和字符串 C++ 第七课 标准 C ...
- python中的glob模块的使用
最近常常用到glob模块,这里做一个简单小结: 用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:”*”, “?”, “[]”.”*”匹配0个或多 ...
- 《团队作业第三、四周》五阿哥小组Scrum 冲刺阶段---Day3
<团队作业第三.四周>五阿哥小组Scrum 冲刺阶段---Day3 一.项目燃尽图 二.项目进展 20182310周烔今日进展: 主要任务一览:界面布局的设计 20182330魏冰妍今日进 ...
- Otsu 类间方差法
又称最大类间方差法.是由日本学者大津(Nobuyuki Otsu)于1979年提出的[1],是一种自适合于双峰情况的自动求取阈值的方法.又叫大津法,简称Otsu. 算法提出初衷是是按图像的灰度特性 ...
- SpringBoot第二节(SpringBoot整合Mybatis)
1.创建Spring Initiallizr项目 一直点击下一步 2.引入依赖 <dependencies> <dependency> <groupId>org.s ...
- Linux+Tomcat环境下安装SSL证书
1.将申请好的证书(4个文件)文件放入/home/tomcat/apache-tomcat-9.0.12/conf/cert文件夹下2.(或者)将申请好的证书(4个文件)文件放入/home/tomca ...
- ·分布式文件系统HDFS 练习
作业要求来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 1.目录操作 在HDFS中为hadoop用户创建一个用户目录( ...
- 剑指offer:扑克牌顺子
题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...