-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdp-coin-change.java
More file actions
81 lines (60 loc) · 2.7 KB
/
dp-coin-change.java
File metadata and controls
81 lines (60 loc) · 2.7 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Given a number of dollars, , and a list of dollar values for distinct coins, , find and print the number of different ways you can make change for dollars if each coin is available in an infinite quantity.
Hints:
You can solve this problem recursively, but you must optimize your solution to eliminate overlapping subproblems using Dynamic Programming if you wish to pass all test cases. More specifically, think of ways to store the checked solutions and use the stored values to avoid repeatedly calculating the same values.
Think about the degenerate cases:
How many ways can you make change for dollars?
How many ways can you make change for less than dollars if you have no coins?
If you are having trouble defining the storage for your precomputed values, then think about it in terms of the base case .
Input Format
The first line contain two space-separated integers describing the respective values of and .
The second line contains space-separated integers describing the respective values of , where each integer denotes the dollar value of a distinct coin available in an infinite quantity.
Constraints
The list of coins contains distinct integers where each integer denotes the dollar value of a coin available in an infinite quantity.
Output Format
Print a single integer denoting the number of ways we can make change for dollars using an infinite supply of our types of coins.
Sample Input 0
4 3
1 2 3
Sample Output 0
4
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static long makeChange(int[] coins, int money) {
if (coins.length == 0) {
return 0;
}
if (money <= 0) {
return 1;
}
//Arrays.sort(coins);
int n = coins.length;
long[] tbl = new long[money + 1];
tbl[0] = 1;
for (int i = 0; i < n; i++) {
for (int j = coins[i]; j <= money; j++) {
tbl[j] += tbl[j - coins[i]];
//System.out.println("Adding " + coins[i] + " coin, value " + j + ", solutions " + tbl[j] + ", money " + money);
}
}
//for (int i = 0; i < tbl.length; i++) {
// System.out.println(i + ":" + tbl[i]);
//}
return tbl[money];
}
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();
}
System.out.println(makeChange(coins, n));
}
}