๐ 322. ้ถ้ฑๅ ๆข
2022ๅนด6ๆ9ๆฅๅฐไบ 1 ๅ้
๐ 322. ้ถ้ฑๅ ๆข
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
public int coinChange(int[] coins, int amount) {
// ๆ่ทฏ๏ผ
// dp[i] = dp[i - [j]] + 1, dp[i]
int len = coins.length;
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for(int i = 0; i <= amount; i ++) {
for(int j = 0; j < len; j ++) {
if(i - coins[j] >= 0) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
if(dp[amount] == amount + 1) {
return -1;
}
return dp[amount];
}
}