点击打开链接
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount+1, INT_MAX);
dp[0] = 0;
for(int i=0; i<coins.size(); i++){
for(int coin=coins[i]; coin<=amount; coin++){
if(dp[coin-coins[i]]!=INT_MAX){
dp[coin] = min(dp[coin], dp[coin-coins[i]]+1);
}
}
}
return dp[amount]==INT_MAX?-1: dp[amount];
}
};