-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCoin_Change.java
63 lines (51 loc) · 1.17 KB
/
Coin_Change.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
54
55
56
57
58
59
60
61
package DynamicProgramming;
import java.io.*;
import java.util.*;
class GfG {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int Arr[] = new int[m];
for (int i = 0; i < m; i++)
Arr[i] = sc.nextInt();
Solution ob = new Solution();
System.out.println(ob.count(Arr, m, n));
}
}
}
class Solution {
public long count(int S[], int m, int n) {
long[][] mat = new long[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
mat[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
mat[0][i] = 0;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (S[i - 1] <= j) {
mat[i][j] = mat[i - 1][j] + mat[i][j - S[i - 1]];
} else {
mat[i][j] = mat[i - 1][j];
}
}
}
return mat[m][n];
// if( n == 0 ){
// return 1;
// }
// if( n < 0 ){
// return 0;
// }
// if( m <= 0 && n >= 1 ){
// return 0;
// }
// return count( S, m-1, n ) + count(S, m, n - S[m-1] );
}
}
// n = 4
// m = {1,2,3}