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 ...
随机推荐
- 设计一个有getMin功能的栈
[说明]: 本文是左程云老师所著的<程序员面试代码指南>第一章中“设计一个有getMin功能的栈”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路,不包含解析说明 ...
- mysql 组合索引
MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能有很多人还不是十分的了解,下面就为您分析两者的主要区别,供您参考学习. 为了形象地对比两者,再建一个表 ...
- appium获取app应用的package和 activity。---新手总结(大牛勿喷,新手互相交流)
从网上搜索的方法: 如下: 1.查看源码 2.日志法a.启动待测apkb.开启日志输出:adb logcat>D:/log.txtc.关闭日志输出:ctrl+cd.查看日志直接搜索 :Displ ...
- selenium 学习笔记 ---新手学习记录(8) 问题总结(java)
1.获取执行js代码后的返回值 //获取滚动距离 String jl="return $('#chapterul li').height();"; Long jlhq=(Long) ...
- 自己动手写List集合(C#)
平时经常使用微软的List集合,觉得理所应当,这阵子突然意识到学编程学这么久,总不能只生存在某个平台某种语言下面.我觉得要跳出这个框,而数据结构是经常用到的,所以呢,作为一个有志向的程序员应该学会它. ...
- 泛型 "new的性能"
完美的.net泛型也有特定的性能黑点?追根问底并且改善这个性能问题 完美的.net真泛型真的完美吗 码C#多年,不求甚解觉得泛型就是传说中那么完美,性能也是超级好,不错,在绝大部分场景下泛型表现简直可 ...
- jquery简单的插件
$(function() { $.fn.插件名称 = function(options) { var defaults = { Event : "click", //触发响应事件 ...
- Protel99 SE快捷键大全
为了方便观看我们的protel99 se视频教程的朋友,我们在这里发布了protel99 se的所有的键盘的快捷分健大全,希望大家在学习我们的视频教程的时候,可以熟悉以下这些快捷键,因为平时我们用pr ...
- Java中volatile的作用以及用法
volatile让变量每次在使用的时候,都从主存中取.而不是从各个线程的“工作内存”. volatile具有synchronized关键字的“可见性”,但是没有synchronized关键字的“并发正 ...
- Flex LinkButton鼠标划过出现下划线
在LinkButton中 textDecoration属性设置label的是否有下划线装饰,属性值分为"none","underline" 代码如下------ ...