经典完全背包,其实有很多优化空间,筛选背包可以用二分直接从合适的硬币开始遍历
import java.util.Arrays;
// 冲刺089
class Solution {
public int coinChange(int[] coins, int amount) {
int len = coins.length;
int[] dp = new int[amount + 1];
Arrays.fill(dp, -1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
int minNum = Integer.MAX_VALUE;
for (int j = len - 1; j >= 0; j--) {
if (i - coins[j] < 0 || dp[i - coins[j]] == -1) {
continue;
}
if (dp[i - coins[j]] + 1 < minNum) {
minNum = dp[i - coins[j]] + 1;
}
}
if (minNum != Integer.MAX_VALUE) {
dp[i] = minNum;
}
}
return dp[amount];
}
}