0322. Coin Change
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
// dp[j]: fewest number of coins to make amount=j
// dp[j] = min(dp[j], dp[j - coins[i]] + 1)
// max will be amount+1 because the min value of coin is 1
// only possible amount of coins is amount
vector<int> dp(amount+1, amount+1);
dp[0] = 0;
for(int coin : coins) {
for(int j = coin; j <= amount; j++) {
dp[j] = min(dp[j], dp[j - coin] + 1);
}
}
if(dp[amount] > amount) return -1;
return dp[amount];
}
};