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. xcodebuild和xcrun实现自动打包iOS应用程序

    随着苹果手持设备用户的不断增加,ios应用也增长迅速,同时随着iphone被越狱越来越多的app 的渠道也不断增多,为各个渠道打包成了一件费时费力的工作,本文提供一种比较智能的打包方式来减少其带来的各 ...

  2. 了解javascript中的事件(二)

    本文目录如下: 零.寒暄 一.事件的分类 二.事件代理 2.1 问题引出 2.2 什么是事件代理 2.3 完整示例 二.事件代理 三.事件代理思想的用处 四.总结 零.寒暄 这篇博客本该出现在两个月以 ...

  3. 图片放大镜插件 Cloud Zoom v3.1

    Cloud Zoom是一个图像放大jQuery插件,效果堪比Magic Zoom.相对于流行jQZoom插件,Cloud Zoom体积小,有更多的功能和更强大的跨浏览器兼容性. 猛击这里查看演示DEM ...

  4. Tomcat漏洞说明与安全加固

    Tomcat是Apache软件基金会的一个免费的.开放源码的WEB应用服务器,可以运行在Linux和Windows等多个平台上,由于其性能稳定.扩展性好.免费等特点深受广大用户的喜爱.目前,互联网上绝 ...

  5. Codeforces Round #266 (Div. 2) D

    D. Increase Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. leetcode course shedule

    题目就不说了,问题本质就是在一个有向图中查找它是不是存在环. 上网百度了一下,方法是,找出图中入度为0 的点,将以它为起点的边去掉. 重复这一动作,直到所有的边都被去掉(没有环)或者存在边但是无法再去 ...

  7. LCIS 最长公共上升子序列

    这个博客好久没写了,这几天为了准备清华交叉研究院的夏令营,在复习大一大二ACM训练时的一些基础算法,正好碰到LICS,发现没有写在博客里,那就顺便记录一下好了. 参考链接:http://blog.cs ...

  8. 快速排序 Quick Sort

    自己写的代码,记录一下.分别记录了两种partition的方法. public class QuickSort { public static void quickSort(int[] nums, i ...

  9. oracle连接数据

    1.源代码 string connString = "User ID=scott;Password=yanhong;Data Source=(DESCRIPTION = (ADDRESS_L ...

  10. Map中如何把没有定义操作符<的类作为key

    Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...