```
public int coinChange(int[] coins, int amount) {
int[] res = new int[amount + 1];
for (int i = 1; i <= amount; i++) {
int min = Integer.MAX_VALUE;
for (int j = 0; j < coins.length; j++) {
if (coins[j] <= i && res[i - coins[j]] != -1) {
min = Math.min(min, res[i - coins[j]] + 1);
}
}
if (min == Integer.MAX_VALUE) {
res[i] = -1;
} else {
res[i] = min;
}
}
return res[amount];
}
```