forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinChange.java
More file actions
30 lines (24 loc) · 820 Bytes
/
coinChange.java
File metadata and controls
30 lines (24 loc) · 820 Bytes
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
public class coinChange {
public static int count( int S[], int m, int n )
{
// table[i] will be storing the number of solutions for
// value i. We need n+1 rows as the table is constructed
// in bottom up manner using the base case (n = 0)
int dp[]=new int[n+1];
// Base case (If given value is 0)
dp[0] = 1;
// Pick all coins one by one and update the table[] values
// after the index greater than or equal to the value of the
// picked coin
for(int i=0; i<m; i++)
for(int j=S[i]; j<=n; j++)
dp[j] += dp[j-S[i]];
return dp[n];
}
public static void main(String[] args) {
int arr[] = {4, 12, 31, 52};
int m = arr.length;
int n = 4;
System.out.println(count(arr, m, n));
}
}