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的更多相关文章

  1. [LeetCode] 518. Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  2. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. [LeetCode] 518. Coin Change 2 硬币找零 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  4. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  5. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  6. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  8. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  9. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

随机推荐

  1. [noi2011]道路修建 树形dp

    这道题可以说是树形dp的入门题,也可以看成是一道检验[树]这个数据结构的题目: 这道题只能bfs,毕竟10^6的复杂度win下肯定爆栈了: 但是最恶心的还不是这个,实测用printf输出 用cout输 ...

  2. tomcat与IIS在多IP服务器下的支持

    同一个服务器下,双IP(或更多IP),实现tomcat与IIS公用80端口. 操作其实也很简单的,首先禁用iis的套接字池,iis绑定一个ip,然后tomcat在绑定另一个ip,最后重启下服务器即可. ...

  3. javascript设计模式-生成器模式(Builder)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 网格导入设置 Import settings for Meshes

    原地址:http://game.ceeger.com/Components/FBXImporter-Model.html The Import Settings for a model file wi ...

  5. 判断一个字符串在至多删除k个字符后是否为回文串

    转自: http://www.careercup.com/question?id=6287528252407808 问题描述: A k-palindrome is a string which tra ...

  6. iOS第三方支付-支付宝支付

    处理手机中有无支付宝的情况支付宝调用接口: - (void)payOrder:(NSString *)orderStr      fromScheme:(NSString *)schemeStr    ...

  7. POJ 1939

    #include<iostream> #include<iomanip> #define MAXN 10000 using namespace std; ]; int main ...

  8. ExtJs之Ext.isEmpty

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  9. HBase保存的各个字段意义解释

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  10. 2013 ACM/ICPC Asia Regional Changsha Online G Goldbach

    比赛的时候,被题目误导了,题目最后说结果可能很大,要取模,那时就想直接求会TLE的!!! 赛后才知道,坑啊………… 代码如下: #include<iostream> #include< ...