-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoin_Change.java
More file actions
37 lines (24 loc) · 768 Bytes
/
Coin_Change.java
File metadata and controls
37 lines (24 loc) · 768 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
30
31
32
33
34
35
36
37
package DynamicAlgorithm;
public class Coin_Change {
public static void main(String[] args) {
int[] Coins = {1, 5, 4, 8, 10};
int TK = 12;
for (int coin : Coins){
System.out.println(coin + "\t");
}
System.out.println("Solution: " + getNumberOfWays(TK , Coins));
}
private static int getNumberOfWays(int i, int[] coins) {
int[] ways = new int[i + 1];
ways[0] = 1;
for (int coin : coins) {
for (int j = 0; j < ways.length; j++) {
if (coin <= j) {
ways[j] += ways[j - coin];
System.out.println("J = "+ j + " ways: " + ways[j]);
}
}
}
return ways[i];
}
}