LeetCode——Coin Change
Question
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
Note:
You may assume that you have an infinite number of each kind of coin.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Solution
动态规划。依次计算出组成1~amount所需要的硬币数目。
dp[i] = min(dp[i], dp[i - coins[j]] + 1),因为是依次求解的,那么求dp[i]的时候,dp[i - coins[j]]已经求解过了。
Code
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.size(); j++) {
if (coins[j] <= i) {
dp[i] = min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] < amount + 1 ? dp[amount] : -1;
}
};
LeetCode——Coin Change的更多相关文章
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- LeetCode Coin Change
原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] 518. Coin Change 2 硬币找零 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/ 题目 ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Restful and 前后端分离---AutoTest newman--postman
http://www.cnblogs.com/zuoshaowei/p/6192863.html https://www.getpostman.com/docs/newman_intro swagge ...
- ora-04021 无法锁表的解决办法
案例场景: 备库上有一张分区表,在做数据导入出了点问题,需要truncate掉重新导入,在执行truncate table时发生了04021错误. 错误分析: ora-04021的解释是等待锁定对象时 ...
- Accuracy, Precision, Resolution & Sensitivity
Instrument manufacturers usually supply specifications for their equipment that define its accuracy, ...
- Systemd 配置
一.开机启动 对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件. 如果你想让该软件开机启动,就执行下面的命令(以httpd. ...
- 4.2 - MySQL
一.表关系 请创建如下表,并创建相关约束 班级表:class 学生表:student cid caption grade_id sid sname gender class_id 1 一年一班 1 1 ...
- Ubutun使用记录——语系错误(转)
add by zhj: 对原文有修改,原文是在创建用户时出现的问题,而我是在使用psql时出现的, 但问题是相同的. 原文:http://www.douban.com/note/362250557/ ...
- Oracle中to_number()函数的用法
to_number()函数是oracle中常用的类型转换函数之一,是将一些处理过的按一定格式编排过的字符串变回数值型的格式. 1.to_number()函数可以将char或varchar2类型的str ...
- python 之操作mysql 数据库实例
对于python操作mysql 数据库,具体的步骤应为: 1. 连接上mysql host 端口号 数据库 账号 密码2. 建立游标3. 执行sql(注意,如果是update,insert,delet ...
- 工作笔记——dom属性巧用
1.获取验证码 dom: <span class="btn btn-primary">获取验证码</span> js: /** *@Author Mona ...
- Bootstrap3的输入框数字点击修改效果
<div class="container"><div class="page-header"><h3>Bootstrap ...