-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChange.java
More file actions
66 lines (52 loc) · 1.77 KB
/
CoinChange.java
File metadata and controls
66 lines (52 loc) · 1.77 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package Dynamic_Programming;
import java.util.Arrays;
public class CoinChange {
// Memoization
public static int change(int amount, int[] coins){
// base case
int n = coins.length;
int ind = n-1;
int[][] dp = new int[n][amount+1];
for (int i=0; i< dp.length; i++){
Arrays.fill(dp[i], -1);
}
return helper(ind, amount, coins, dp);
}
public static int helper(int ind, int amount, int[] coins, int[][] dp){
if (ind == 0){
if (amount % coins[ind] == 0) return 1;
else return 0;
}
if (dp[ind][amount] != -1) return dp[ind][amount];
int notTake = helper(ind-1, amount, coins, dp);
int take = 0;
if (coins[ind] <= amount){
take = helper(ind, amount-coins[ind], coins, dp);
}
return dp[ind][amount] = take + notTake;
}
public static int changeTab(int T, int[] coins){
int n = coins.length;
int[][] dp = new int[n][T+1];
// base case
for (int t=0; t<T+1; t++){
if (t % coins[0] == 0) dp[0][t] = 1;
else dp[0][t] = 0;
}
for (int i=1; i<dp.length; i++){
for (int j=0; j<dp[0].length; j++){ // j is T at each Column
int take = 0;
if (coins[i] <= j) take = dp[i][j-coins[i]];
int notTake = dp[i-1][j];
dp[i][j] = take + notTake;
}
}
return dp[n-1][T];
}
public static void main(String[] args) {
int[] coins = {1, 2, 3};
int amount = 4;
System.out.println(change(amount, coins));
System.out.println(changeTab(amount, coins));
}
}