-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinCoinNum.java
More file actions
58 lines (51 loc) · 1.49 KB
/
Copy pathMinCoinNum.java
File metadata and controls
58 lines (51 loc) · 1.49 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
/* If we have a number of coins with value 1, 3 and 5. What is the minimum
* number of coins to get 11?
*/
import java.io.*;
import java.util.*;
public class MinCoinNum {
public static void main(String[] args) {
int[] values = {1, 3, 5};
solve(values, 11);
}
public static void solve(int[] values, int sum) {
int[] d = new int[sum+1];
int[] path = new int[sum+1];
d[0] = 0;
path[0] = -1;
for(int i=1; i<=sum; i++) {
int min = Integer.MAX_VALUE;
int p = -1;
boolean flag = false;
for(int j=0; j<values.length; j++) {
if(values[j]<=i) {
if(d[i-values[j]]==0 && i-values[j]>0)
continue;
if((d[i-values[j]]+1) < min) {
min = d[i-values[j]]+1;
p = i-values[j];
flag = true;
}
}
}
if(!flag) {
d[i] = 0;
}
else {
d[i] = min;
path[i] = p;
}
}
if(d[sum]>0) {
System.out.println(d[sum]);
int a = sum;
while(path[a] >= 0) {
System.out.print(a - path[a] + " ");
a = path[a];
}
System.out.println();
} else {
System.out.println("Do not have solution!");
}
}
}