-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoin_322.java
41 lines (34 loc) · 1.33 KB
/
Coin_322.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Arrays;
class Coin_322 {
// My first solution: Bottom-up DP (similar with standard solution #3)
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i <= amount; i++)
for (int coin : coins)
if (coin <= i && dp[i - coin] < Integer.MAX_VALUE)
dp[i] = Math.min(dp[i], 1 + dp[i - coin]);
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
// My first solution: Top-down DP (similar with standard solution #2)
/*
public int coinChange(int[] coins, int amount) {
int[] memo = new int[amount + 1];
coinChange(coins, amount, memo);
return memo[amount] == Integer.MAX_VALUE ? -1 : memo[amount];
}
private int coinChange(int[] coins, int amount, int[] memo) {
if (amount == 0) return 0;
if (memo[amount] != 0) return memo[amount];
memo[amount] = Integer.MAX_VALUE;
for (int coin : coins)
if (coin <= amount) {
int temp = coinChange(coins, amount - coin, memo);
if (temp < Integer.MAX_VALUE)
memo[amount] = Math.min(memo[amount], 1 + temp);
}
return memo[amount];
}
*/
}