-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChange.java
More file actions
54 lines (48 loc) · 1.48 KB
/
Copy pathCoinChange.java
File metadata and controls
54 lines (48 loc) · 1.48 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
package CrackingCodeInterview;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CoinChange {
static long[][] mem;
public long partWays(int[] coins,int n, int minCoin){
if(n<0){
// System.out.println("OH no negative");
return 0;
}
else if(n==0){
// System.out.println("Got one");
return 1;
}
else if(mem[n][minCoin]!=-1) {
//System.out.println("GOt from mem " + n + " is part in ways " + mem.get(n));
return mem[n][minCoin];
}
else{
long ways=0;
for(int i=minCoin;i<coins.length;i++){
// System.out.println("Considering "+ coins[i] + " for "+n+ " with coin min: "+ minCoin);
ways+=partWays(coins,n-coins[i],i);
//System.out.println();
}
mem[n][minCoin]=ways;
return ways;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int coins[] = new int[m];
for(int coins_i=0; coins_i < m; coins_i++){
coins[coins_i] = in.nextInt();
}
mem= new long[n+1][m];
for(int i=0;i<=n;i++){
for(int j=0;j<m;j++){
mem[i][j]=-1;
mem[0][j]=1;
}
}
System.out.println((new CoinChange()).partWays(coins,n,0));
}
}