322. 零钱兑换

O(nm)直接超时,虽然也是动态规划

查看代码
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        if(amount == 0){
            return 0;
        }
        vector<int>temp(amount+1,-1);
        temp[0] = 0;
        for(int i=1; i<=amount;i++){
            int min_temp = temp[i];
            for(int j=0; j<coins.size();j++){
                if(i-coins[j]<0 || temp[i-coins[j]] == -1){
                    continue;
                }
                else{
                    if(min_temp == -1){
                        min_temp = temp[i-coins[j]]+1;
                        
                    }
                    else{
                       // cout<<min_temp;
                        min_temp = min(min_temp, temp[i-coins[j]]+1);
                    }
                }
            }
            temp[i] = min_temp;
        }
        return temp[amount];
    }
};

看题解咯,发现没问题,就是cout太消耗时间了

 

posted @ 2021-12-27 15:10  jozon  阅读(9)  评论(0)    收藏  举报