[Vssue]0322.零钱兑换.md
youngyangyang04 opened this issue · 4 comments
youngyangyang04 commented
359702798 commented
感觉好难想到啊 判断dp[j-coins[i]
Du1in9 commented
Du1in9 commented
class Solution {
public int coinChange(int[] coins, int w) {
int[] dp = new int[w + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 0; i < coins.length; i++) {
for (int j = coins[i]; j <= w; j++) {
if (dp[j - coins[i]] != Integer.MAX_VALUE) {
dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
}
}
}
if (dp[w] == Integer.MAX_VALUE) return -1;
else return dp[w];
}
}