```
public int coinChange(int[] coins, int amount) {
if (amount == 0) return 0;
int[] dp = new int[amount + 1];
Arrays.sort(coins);
for (int i = 0; i <= amount; ++i) {
dp[i] = i % coins[0] == 0? i / coins[0] : Integer.MAX_VALUE - 1;
}
for (int i = 1; i < coins.length; ++i) {
for (int j = 1; j <= amount; ++j) {
if (j >= coins[i]) dp[j] = Math.min(1 + dp[j - coins[i]], dp[j]);
else dp[j] = dp[j];
}
}
return dp[amount] == Integer.MAX_VALUE - 1? -1 : dp[amount];
}
```