美文网首页
Coin Change 2

Coin Change 2

作者: 极速魔法 | 来源:发表于2017-07-11 21:50 被阅读14次

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

Note: You can assume that

0 <= amount <= 5000
1 <= coin <= 5000
the number of coins is less than 500
the answer is guaranteed to fit into signed 32-bit integer
Example 1:

Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:

Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.
Example 3:

Input: amount = 10, coins = [10]
Output: 1

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

class Solution {
public:
    int change(int amount, vector<int>& coins) {
        vector<vector<int>> memo(coins.size(),vector<int>(amount+1,0));
        sort(coins.begin(),coins.end());
        int row=coins.size();
        if(row==0 && amount==0){
            return 1;
        }

        if(row==0 && amount>0){
            return 0;
        }
        int column=amount+1;
        
        assert(coins[0]>0);
        //init 
        if(coins[0]==1) {
            for (int j = 0; j < memo[0].size(); j++) {
                memo[0][j] = 1;
            }
        } else{
            for(int j=0;j<memo[0].size();j++){
                if(j%coins[0]==0){
                    memo[0][j]=1;
                } else{
                    memo[0][j]=0;
                }
            }
        }
        
        

        for(int i=1;i<coins.size();i++){
            for(int j=0;j<amount+1;j++){
                int k=0;
                while(j-coins[i]*k>=0){
                    memo[i][j]+=memo[i-1][j-coins[i]*k];
                    k++;
                }
            }
        }

        return memo[row-1][amount];

    }
};

int main(){
    int amount=0;
    int coins[]={};

    vector<int> vec(coins,coins+sizeof(coins)/sizeof(int));
    cout<<Solution().change(amount,vec)<<endl;
    return 0;
}

相关文章

  • Coin Change 2

    题目来源给一个钱数以及一些硬币,硬币可以无限取,问有多少种组合方案。一看就是一道DP题,但是太久没刷题,手生。导致...

  • Coin Change 2

    You are given coins of different denominations and a tota...

  • leetcode-12

    Coin Change Boundary: There may be no possible change, so...

  • Coin Change

    题目You are given coins of different denominations and a to...

  • Coin Change

    题目来源一道简单的DP题,n种硬币,要求组成某个数值的硬币数最少,代码如下: 看了下讨论区,感觉可以写的更简洁一下...

  • coin change

    最简单的DP

  • coin change

    You are given coins of different denominations and a tota...

  • 动态十一:零钱兑换 II

    题目地址:* https://leetcode-cn.com/problems/coin-change-2/[h...

  • coin change 2解题报告

    Description: You are given coins of different denominatio...

  • 518. Coin Change 2

    思路 动态规划 用一个数组dp存储结果,dp[i]表示amount=i时的组合数目。 初始态:dp[0]=1 遍历...

网友评论

      本文标题:Coin Change 2

      本文链接:https://www.haomeiwen.com/subject/ppfvhxtx.html