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 ...
随机推荐
- VS2010使用DX报错 VS报错之混合模式程序集是针对“v1.1.4322”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
更改项目的app.config内容为以下内容 目的是开启对低版本的NETFWK支持 其实出现混合模式集的问题不只是在V1.1.4322这个版本上,在查询解决方案时发现,但凡程序集版本发生改变时,都有可 ...
- poj 3764 The xor-longest Path Trie
题目链接 求树上的一条最长异或路径. 定义f(u, v)为u到v的路径, 那么显然f(1, u)^f(1, v) = f(u, v), 想不到这个就没有办法做. 然后就可以用字典树查询+插入了. 用指 ...
- java 类排序
参考文档:Java排序: Comparator vs Comparable 入门 java类经常面临排序问题,据我所知:java提供comparable和comparator两种比较方式: 1. co ...
- Android UiAutomator 自动化测试编译运行---新手2
1.首先打开eclipse创建java项目
- c 语言 指针 与地址
1.如何实现交换两个数的值 void swap( int *a,int *b) { int tep=*a;//*a其实就是主函数a的值,a是主函数存a数值的地址. *a =*b; *b =tep; ...
- Bootstrap 响应式瀑布流 (使用wookmark)
使用瀑布布局 官方 http://www.wookmark.com/jquery-plugin GitHub https://github.com/GBKS/Wookmark-jQuery (下载后 ...
- Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)
最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...
- Silverlight CheckBoxList
项目要用到复选框,可是在Silverlight中不存在CheckBoxList.通过查阅资料以及依据自己的理解,写了简单演示样例: 1.XAML <UserControl x:Class=&qu ...
- C++设计模式之状态模式(四)
4.状态模式总结 状态模式将一个对象在不同状态下的不同行为封装在一个个状态类中,通过设置不同的状态对象能够让环境对象拥有不同的行为.而状态转换的细节对于client而言是透明的.client不直接操作 ...
- Connection for controluser as defined in your configuration failed.
在mysql中使用事件调度器(计划任务), 语句写好了,运行也ok,可是却没有预期的结果.网上总结了非常多计划任务失效的原因.没有一种适合我. 在phpmyadmin中打开事件表,发现以下一串红色的提 ...