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];
}
};
标签:return,int,maxs,coins,C++,amount,vector,Coin,Change 来源: https://blog.csdn.net/coolsunxu/article/details/119882248