leetcode 322. Coin Change

原创 2016年06月02日 01:03:32

题目描述:
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.

很简单的动态规划,其中dp[i]表示和为i所需要的最少的硬币数,
ac代码:

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        if(amount == 0)
            return 0;
        if(coins.size() == 0)
            return -1;
        int dp[amount+1] = {0};
        for(int i = 0; i < coins.size(); i++){
            if(coins[i] <= amount)
                dp[coins[i]] = 1;
        }
        for(int i = 1; i < amount; i++){
            if(dp[i] == 0)
                continue;
            for(int j = 0; j < coins.size(); j++){
                int tmp = coins[j] + i;
                if(tmp <= amount){
                    if(dp[tmp] == 0)
                        dp[tmp] = dp[i] + 1;
                    else
                        dp[tmp] = min(dp[i]+1, dp[tmp]);
                }
            }
        }
        if(dp[amount])
            return dp[amount];
        else
            return -1;
    }
};

另附上leetcode上的题解,上面有各种解法的比较和分析(暴力解法,自顶向下的dp和自底向上的dp)

版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode] 322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function...
  • TstsUgeg
  • TstsUgeg
  • 2015年12月28日 11:33
  • 1337

[LeetCode 322] Coin Change

You are given coins of different denominations and a total amount of money amount. Write a functio...
  • sbitswc
  • sbitswc
  • 2016年01月12日 16:22
  • 2667

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Quest...
  • bruce128
  • bruce128
  • 2016年03月17日 01:11
  • 1599

leetcode 322. Coin Change-硬币交换|动态规划

原题链接:322. Coin Change 【思路】 本题考查动态规划。也许一开始很容易想到用贪心算法,但是贪心算法在某些情况下是不成立的,比如coins = [1, 3, 5, 6],要amount...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016年03月24日 23:19
  • 5481

322. Coin Change(C++实现)

递归算法: class Solution { int *dp; vector a; int dfs(int S) { if(dp[...
  • jingmiaa
  • jingmiaa
  • 2016年05月23日 21:07
  • 352

322. Coin Change LeetCode

题意:给出硬币的种类和要换的钱,最少可以用多少个硬币去换,不能返回-1。 题解:dp,类似背包的dp,dp[i]表示换成i元的时候最少要多少个硬币。 dp[v] = min(dp[v],dp[v ...
  • youthinkwu
  • youthinkwu
  • 2016年03月16日 18:19
  • 86

LeetCode *** 322. Coin Change

题目: You are given coins of different denominations and a total amount of money amount. Write a fu...
  • treeshy
  • treeshy
  • 2016年04月23日 19:15
  • 85

【LeetCode】322. Coin Change

Difficulty: Medium You are given coins of different denominations and a total amount of money a...
  • Juxin_Lin
  • Juxin_Lin
  • 2016年11月01日 20:35
  • 104

LeetCode—322. Coin Change

322. Coin Change Total Accepted: 837 Total Submissions: 3093 Difficulty: Medium You are gi...
  • corpsepiges
  • corpsepiges
  • 2015年12月28日 09:33
  • 399

LeetCode[518]Coin Change 2(Java)

You are given coins of different denominations and a total amount of money. Write a function to comp...
  • sunday0904
  • sunday0904
  • 2017年10月08日 20:05
  • 217
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 322. Coin Change
举报原因:
原因补充:

(最多只允许输入30个字)