CoinChange
题目
题目:CoinChange
有面额不等的coins,数量无限,要求以最少的\(coins\)凑齐所需要的\(amount\)。
若能,返回所需的最少coins的数量,若不能,返回-1。
- Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1) - Example 2:
coins = [2], amount = 3
return -1.
无法用贪心做,例如:coins = [5,6,10], amount = 11 *
。
动态规划解法:
1、
这里我用\(coins=[c_1,c_2,...,c_m]\)表示所有的\(coin\)面值的集合;
用集合\(S(amount)=(c_{i1},c_{i2}...c_{im})\)表示凑齐\(amount\)的一种凑法;
用函数\(f(amount)\)表示 凑齐\(amount\)的所有的凑法。
当\(amount=i\)的时候,有\(f(i)\)种凑法,
$ f(amount) = \{ (c_{i1},...),(c_{i2},...),...,(c_{ik},...) \} ,c_x\in coins$
例如:
\(coins = [1,5,6,9], amount = 11\);
.........\(f(11)=\{ (5,6),(1,1,9)\}\)凑齐11,有两种办法,一是5+6,另一种是1+1+9.
用\(dp[i]\)表示凑齐\(amount\)所需的最少\(coins\)数,\(dp[i]=-1\)表示无法凑齐。
\(dp[i] = min \{count( f(i) ) \}\)
例如:\(dp[11]=min\{count(f(11))\}=min\{count((5,6),(1,1,9))\}=min\{2,3\}=2\)
2、递推公式:
\(f()\)的递推公式为:
\(f(j)=\{f(i_1)+c_1,f(i_2)+c_2,...,f(i_k)+c_k \}\),条件:\(j>i,f(j)>=0,c_x\in coins\)
\(dp[]\)的递推公式为:
\(dp[j]=min\{dp[i_1]+1,dp[i_2]+1,...,dp[i_k]+1 \}\)
3、边界条件:
\(f(0)=0\)
\(dp[0]=0\)
例子:
amount: 11
coins: 0 - - - - 5 6 - - - 10 -
amount: 0 1 2 3 4 5 6 7 8 9 10 11
dp: 0 - - - - 1 1 - - - 1 2
代码:
int coinChange(const vector<int>& coins, int amount) {
vector<int> dp(amount + 1, -1);
dp[0] = 0;
for (int i = 1; i <= amount ; i++) {
for (int c: coins) {
if (i >= c && dp[i-c] >= 0) { // 若coin面值超过amount,无法凑出
if (dp[i] > 0) { // 若有别的凑法,比较那种凑法用的coins少
dp[i] = dp[i] < (dp[i - c] + 1) ? dp[i] : dp[i - c] + 1;
} else {
dp[i] = dp[i - c] + 1;
}
}
}
}
//display(dp);
return dp[amount];
}
CoinChange的更多相关文章
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- LeetCode Coin Change
原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- python面试2
Python语言特性 1 Python的函数参数传递 看两个例子: 1 2 3 4 5 a = 1 def fun(a): a = 2 fun(a) print a # 1 1 2 ...
- Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode:Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
随机推荐
- Oracle10g--plSql命令
每天学点Oracle10g--plSql命令 附录B SQL*PLUS Sql*plus 中使用绑定变量: sql> variable x number; sql> exec :x := ...
- Liunx 环境下vsftpd的三种实现方法(超详细参数)
以下文章介绍Liunx 环境下vsftpd的三种实现方法 ftp://vsftpd.beasts.org/users/cevans/vsftpd-2.0.3.tar.gz,目前已经到2.0.3版本.假 ...
- 基于Visual C++2013拆解世界五百强面试题--题7-链表的各种操作
请用C实现一个链表,实现链表的查找,逆置,替换,删除,添加,清空,创建. 查找.替换和删除.添加里面都会用到遍历链表的操作,所以重点在于遍历, 链表的逆置和清空考虑到效率,我们可以用递归实现, 至于创 ...
- java学习之JDBC
之前学习了数据库原理,上学期也学了oracle数据库,我的学习视频上是讲的mysql数据库,其实都差不多,复习了下sql知识,数据库的学习就没有写下来了,就从Java怎么操作数据库开始吧. 因为这年过 ...
- js动态加载控件jsp页面
例子1:(具体参照drp中的flow_card_add.jsp)<script> var rowIndex = 0; function addOneLineOnClick() ...
- http://download.qt-project.org/archive/qt/4.5/qt-all-opensource-src-4.5.2.tar.bz2
Index of /archive/qt/4.5 http://download.qt-project.org/archive/qt/4.5/qt-all-opensource-src-4.5.2.t ...
- EJB3.0开发环境的搭建
EJB Container的介绍SUN公司正式推出了EJB的规范之后,在众多的公司和开发者中引起了非常大的反响.标志着用Java开发企业级应用系统将变的非常easy.很多公司都已经推出了或正打算EJB ...
- 阵列中条带(stripe)、stripe unit
摘抄:http://blog.sina.com.cn/s/blog_4a362d610100aed2.html 在磁盘阵列中,数据是以条带(stripe)的方式贯穿在磁盘阵列所有硬盘中的.这种数据的分 ...
- 在mysql中创建存储过程出现1307错误,解决方法
需要删除mysql数据库下proc表 在重新创建 CREATE TABLE `proc` ( `db` char(64) character set utf8 collate utf8_bin NOT ...
- 解决:Visual Assist X 不支持HTML、Javascript等提示
Visual Assist X 安装后,不能进行javascript hmtl提示,只有回到老版本才行.这个问题折腾了老久,才给解决了. 记录下来,以便于网友和自己使用. 问题原因: Visual A ...