class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount == 0) return 0;
vector<int> dp(amount+1, -1);
for(auto coin : coins) if(coin <= amount) dp[coin] = 1;
for(int ii = 1; ii <= amount; ii++) {
if(dp[ii] != -1) continue;
for(auto coin : coins) {
if(ii - coin > 0 && dp[ii - coin] != -1) {
if(dp[ii] == -1) dp[ii] = dp[ii - coin] + 1;
else dp[ii] = min(dp[ii], dp[ii - coin] + 1);
}
}
}
return dp[amount];
}
};
【leetcode 动态规划 C++】322. Coin Change
猜你喜欢
转载自blog.csdn.net/m0_37454852/article/details/114218305
今日推荐
周排行