Coin Change(C++零钱兑换)


(1)dp,完全平方数类似

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int maxs=amount+1;
        int n=coins.size();

        vector<int> v(amount+1,maxs);
        v[0]=0;
        for(int i=1;i<amount+1;i++) {
            for(int j=0;j<n;j++) {
                if(i>=coins[j])
                v[i]=min(v[i],v[i-coins[j]]+1);
            }
        }
        return v[amount]==maxs?-1:v[amount];
    }
};

上一篇:GitLab环境搭建


下一篇:【LeetCode】322. 零钱兑换