322. Coin Change
brute force
DFS + memo (top down)
DP (bottom up
subproblem & dp array:


new version
Last updated


Last updated
class Solution {
public int coinChange(int[] coins, int amount) {
return dfs(coins, amount, new Integer[amount+1]);
}
private int dfs(int[] coins, int amount, Integer[] memo) {
if (amount == 0) { // no more amount, should return 0
return 0;
}
if (amount < 0) {
return -1;
}
if (memo[amount] != null) {
return memo[amount];
}
int result = Integer.MAX_VALUE;
for (int coin : coins) {
int numbers = dfs(coins, amount - coin, memo);
if (numbers == -1) {
continue;
}
result = Math.min(result, 1 + numbers);
}
return memo[amount] = (result == Integer.MAX_VALUE ? -1 : result);
}
}class Solution {
public int coinChange(int[] coins, int amount) {
return dp(coins, amount, new Integer[amount+1]);
}
private int dp(int[] coins, int amount, Integer[] memo) {
if (amount == 0) {
return 0;
}
if (amount < 0) {
return -1;
}
if (memo[amount] != null) {
return memo[amount];
}
int result = Integer.MAX_VALUE;
for (int coin : coins) {
int sub = dp(coins, amount - coin, memo);
if (sub == -1) { // key point!
continue;
}
result = Math.min(result, sub + 1);
}
return memo[amount] = (result != Integer.MAX_VALUE ? result: -1);
}
}
// thought ->
/*
fewest number of coins
subproblem -> f(10), f(9), f(6) , these are all dependent
f(11) = f(10) + 1 (if choose 1)
f(11) = f(9) + 1 (if choose 2)
f(11) = f(6) + 1 (if choose 5)
f(11)
/
f(10) 9. 6
/ \\ /
9 8 5 8
k : how many kind of coins type
k^n , n subproblems
*/
// use memo -> k^n to k*n
class Solution {
public int coinChange(int[] coins, int amount) {
int dp[] = new int[amount+1]; // 初始結果都是amount+1
Arrays.fill(dp, amount+1);
dp[0] = 0;
for (int i = 0; i < amount+1; i++) {
for (int j = 0; j < coins.length; j++) {
if (i - coins[j] >= 0) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return (dp[amount] > amount) ? -1 : dp[amount];
// 初始amount+1,所以如果比 amount 大, 代表沒找到
}
} for (int i = 0; i < amount+1; i++) {
for (int j = 0; j < coins.length; j++) {
if (i - coins[j] >= 0) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount+1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int i = 0; i < dp.length; i++) {
for (int coin : coins) {
if (i - coin >= 0) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] != amount + 1 ? dp[amount] : -1;
}
}