leetcode:Coin Change
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.
分析:题意为给你不同面值的硬币和总金额数,写一个函数去计算构成这个总金额所需的最小的硬币数目。如果硬币的任何组合都不能构成总钱数就返回-1。
思路:很明显的DP问题,一开始在想是否可以使用贪心算法,发现这样是不能保证最小coin数目的,例如:amount = 8, coins为[1, 3, 5, 6],采用贪心策略得到3(6,1,1),而实际上正确值为2(5,3),之所以贪心法在这里不适用是因为贪心所作的决策并不能确保全局最优,如果换作问题为提水,每桶都有一定量的水,怎样才能最少次数运完所有的水,这样可以用贪心选择最多的水,因为每次提越多的水,到最后的次数肯定最少。
本题两种解决办法,但是思想是一样的:
1、使用线性规划法,dp[n]为amount为n的change数目,那么我们从dp[1]开始就可以DP到dp[n],迭代关系式为,dp[n] = min(dp[n], dp[n-coins[m]]+1).
2、使用递归的方法,不过有可能会造成memory exceed,递推关系为count(n,m,coins) = min(count(n,m-1,coins), count(n-coins[m],m,coins)); 其中count表示寻找最少change的函数,n为amount,m为coins(排好序的)的下标。
代码:
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(!coins.size() && amount)
return -1;
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (auto coin : coins) {
for (int i = coin; i <= amount; ++i) {
if (dp[i-coin] != INT_MAX) {
dp[i] = min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] == INT_MAX?-1:dp[amount];
}
};
参考方法2:the first recursive DFS solution
Just use the array to record the previous computed states.
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount<1) return 0;
vector<int> dp(amount, 0);
return help(coins, amount, dp);
}
int help(vector<int>& coins, int remain, vector<int>& dp){
if(remain<0) return -1;
if(remain==0) return 0;
if(dp[remain-1]!=0) return dp[remain-1];
int min=INT_MAX;
for(int coin : coins){
int result=help(coins, remain-coin, dp);
if(result>=0 && result<min)
min=1+result;
}
dp[remain-1]=(min==INT_MAX ? -1 : min);
return dp[remain-1];
}
};
leetcode:Coin Change的更多相关文章
- [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@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- [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 OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
随机推荐
- jQuery - AJAX (keep for myself)
1. 简介:AJAX工作原理图 AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术.(如google地图) 目的:在不重载整个网页的情况下,AJAX 通 ...
- [bzoj 2097]奶牛健美操
题目描述 对于一棵n个点的树,删除k条边,使得所有联通块直径最大值最小 题解 首先二分联通块直径最大值的最小值. 那么这个能否达成的判定变成了一个类似树形dp的东西 对于一个子树,删除一条边可以删除整 ...
- Linux --windows vs
我其实并不是很清楚我在做什么....希望做完之后可以解答....... 在看了一堆GNU, Clang, GCC, QT, MinGW, CygWin, POSIX 这些概念之后,我觉得我在做的事情就 ...
- Matlab命令系列之目录操作
Matlab命令系列之目录操作 filesep 用于返回当前平台的目录分隔符,Windows是反斜杠(),Linux是斜杠(/).有时此命令结合ispc命令使用,可以灵活的设置目录分割符. fullf ...
- 编写更好的CSS
编写好的CSS代码能提升页面的渲染速度.本质上,一条规则都没有引擎解析的最快.MDN上将CSS选择符归拆分成四个主要类别,如下所示,性能依次降低. ID 规则 Class 规则 标签规则 通用规则 对 ...
- PHP PDO_MYSQL 操作类 YAF嵌入高性能类而准备
https://github.com/indieteq/PHP-MySQL-PDO-Database-Class PHP-PDO-MySQL-Class A PHP MySQL PDO class s ...
- ES6中的高阶函数:如同 a => b => c 一样简单
作者:Sequoia McDowell 2016年01月16日 ES6来啦!随着越来越多的代码库和思潮引领者开始在他们的代码中使用ES6,以往被认为是"仅需了解"的ES6特性变成了 ...
- Javascript Date类常用方法详解
getDate :得到的是今天是 几号(1-28.29.30.31). getDay : 得到的是今天是 星期几(1-7). getFullYear : 得到的是今天是 几几年(4位). getH ...
- CodeSite使用小结 转载
一.要使用Codesite,需要引用csintf单元二.CodeSite的基本方法(一)AddCheckPoint方法codesite.AddCheckPoint 加入监测点(二)AddSeperat ...
- POJ 1547
#include<iostream> #include<string> using namespace std; int main() { int length; int hi ...