-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCoin_Changing_Number_of_ways_to_get_total.java
53 lines (46 loc) · 1.43 KB
/
Coin_Changing_Number_of_ways_to_get_total.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
42
43
44
45
46
47
48
49
50
51
52
53
/*
for visualization visit this link
https://www.youtube.com/watch?v=_fgjrs570YE&index=11&list=PLrmLmBdmIlpsHaNTPP_jHHDx_os9ItYXr
*/
public class CoinChanging {
public int numberOfSolutions(int total, int coins[]){
int temp[][] = new int[coins.length+1][total+1];
for(int i=0; i <= coins.length; i++){
temp[i][0] = 1;
}
for(int i=1; i <= coins.length; i++){
for(int j=1; j <= total ; j++){
if(coins[i-1] > j){
temp[i][j] = temp[i-1][j];
}
else{
temp[i][j] = temp[i][j-coins[i-1]] + temp[i-1][j];
}
}
}
return temp[coins.length][total];
}
/**
* Space efficient DP solution
*/
public int numberOfSolutionsOnSpace(int total, int arr[]){
int temp[] = new int[total+1];
temp[0] = 1;
for(int i=0; i < arr.length; i++){
for(int j=1; j <= total ; j++){
if(j >= arr[i]){
temp[j] += temp[j-arr[i]];
}
}
}
return temp[total];
}
//driver
public static void main(String args[]){
CoinChanging cc = new CoinChanging();
int total = 15;
int coins[] = {3,4,6,7,9};
System.out.println(cc.numberOfSolutions(total, coins));
System.out.println(cc.numberOfSolutionsOnSpace(total, coins));
}
}