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太消耗时间了