[leetcode]518. Coin Change 2

[leetcode]518. Coin Change 2


Analysis

还没工作就想退休—— [每天刷题并不难0.0]

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.
在这里插入图片描述

Explanation:

万恶的动态规划!!!

Implement

class Solution {
public:
    int change(int amount, vector<int>& coins) {
        int len = coins.size();
        vector<vector<int>> dp(len+1, vector<int>(amount+1));
        for(int i=0; i<=len; i++)
            dp[i][0] = 1;
        for(int i=1; i<=len; i++){
            for(int j=1; j<=amount; j++){
                dp[i][j] = dp[i-1][j] + (j>=coins[i-1]?dp[i][j-coins[i-1]]:0);
            }
        }
        return dp[len][amount];
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/86701107