class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int currentAmount = 1; currentAmount <= amount; currentAmount++) {
for (int coin : coins) {
int leftAmount = currentAmount - coin;
if (leftAmount < 0 || dp[leftAmount] == Integer.MAX_VALUE) {
continue;
}
dp[currentAmount] = Math.min(dp[leftAmount] + 1 , dp[currentAmount]);
}
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}
322. 零钱兑换
最新推荐文章于 2024-07-25 10:18:09 发布