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 ...
随机推荐
- codility上的练习(5)
codility出了lesson 5了. (1) 合法括号序列,包括( [ { ) ] }这6种字符的字符串,长度N在[0..200000]范围内,为其是否合法. 要求时间复杂度O(N),空间复杂度O ...
- Android 开发笔记 “Sqlite Cursor 使用”
使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿出来谈, ...
- win7 64位的PHP5.4安装redis扩展
先看phpinfo.php信息 可以看是 PHP5.4 VC9 TS Architecture x86 说明是x86的PHP,虽然系统是64位的,所以还是要下载x86的redis 然后Github下载 ...
- CreateFile,ReadFile等API详解(或者说MSDN的翻译)
一.*****CreateFile***** 这个函数可以创建或打开一个对象的句柄,凭借此句柄就可以控制这些对象:控制台对象.通信资源对象.目录对象(只能打开).磁盘设备对象.文件对象.邮槽对象.管道 ...
- Qt在Linux环境下应用程序字体模糊的解决方法(先改成使用默认字体,然后使用qtconfig配置)
这两天一直在用Qt实现一个跨平台的软件.软件之前在Windows上编写的,后来放到里Ubuntu 10.10下编译.程序运行时遇到一个很棘手的问题,界面文本非常模糊.后来在网上查阅了好几天的资料,经历 ...
- Oracle select into from 和 insert into select
select into from SQLSERVER 创建表: select * into aaa from bbb Oracle 创建表: create table aaa as select * ...
- [转]关于SQL分页存储过程的分析
[转]关于SQL分页存储过程的分析 建立一个 Web 应用,分页浏览功能必不可少.这个问题是数据库处理中十分常见的问题.经典的数据分页方法是:ADO 纪录集分页法,也就是利用ADO自带的分页功能(利用 ...
- Sicily-1156
一. 模仿树的的先序遍历.范围是1000个节点.用数组存储节点的信息. 二. 要注意的是,头结点是不确定的,所以在前序遍历之前要找出头结点,除了头结点的下标值出现一次之外,其他结点 ...
- IIS MIME的 映射 网站有些类型的文件不能通过网页访问
在iis中能够浏览所有扩展名的文件时,IIS MIME的 映射 您只能在故障排除过程中将通配符映射添加到 IIS MIME 映射中,以作为一种临时解决方案.确定缺少 MIME 类型是问题的原因后,请删 ...
- Chrome插件Visual Event查看Dom元素绑定事件的利器
找这工具找了好久,统一找着了,开发人员不可多得的好东东,收藏做一下分享. 用Chrome插件Visual Event查看Dom绑定的事件 Visual Event简介 Visual Event是一个开 ...