class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
int INT_MAX = amount + 1;
for (int i = 1; i < dp.length; i++) {
dp[i] = INT_MAX;
for (int coin : coins) {
if (i - coin > -1 && dp[i - coin] != -1) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
if (dp[amount] > amount) {
return -1;
}
return dp[amount];
}
}
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
for (int i = 1; i < dp.length; i++) {
int minDj = -1;
for (int coin : coins) {
if (i - coin > -1 && dp[i - coin] != -1) {
if (minDj == -1) {
minDj = dp[i - coin];
} else {
minDj = Math.min(minDj, dp[i - coin]);
}
}
}
if (minDj == -1) {
dp[i] = -1;
} else {
dp[i] = minDj + 1;
}
}
return dp[amount];
}
}