-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thieves.java
62 lines (50 loc) · 1.49 KB
/
Thieves.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
62
package Dynamic;
import java.util.concurrent.ThreadLocalRandom;
public class Thieves {
public static void main(String[] args) throws Exception {
for (int size = 10; size < 1000000; size*=2) {
int[] values = new int[size];
int[] weights = new int[size];
for (int i = 0; i < size; i++) {
values[i] = ThreadLocalRandom.current().nextInt(10, 99);
weights[i] = ThreadLocalRandom.current().nextInt(10, 99);
}
int maxWeight = 25*size;
long t1 = System.currentTimeMillis();
Thieves t = new Thieves(values, weights, maxWeight);
long t2 = System.currentTimeMillis();
long time = t2-t1;
System.out.println(size+"\t"+time);
}
}
private int[] v;
private int[] w;
private int k;
private int n;
private int[][] solution;
public Thieves(int v[], int w[], int k) {
this.v = v;
this.w = w;
this.k = k;
this.n = w.length;
this.solution = new int[n + 1][k + 1];
// Initialization
for (int j = 0; j <= k; j++) {
solution[0][j] = 0;
}
for (int i = 0; i <= n; i++) {
solution[i][0] = 0;
}
for (int item = 1; item <= n; item++) {
for (int weight = 1; weight <= k; weight++) {
if (w[item - 1] <= weight) {
solution[item][weight] = Math.max(v[item - 1] + solution[item - 1][weight - w[item - 1]],
solution[item - 1][weight]);
} else {
solution[item][weight] = solution[item - 1][weight];
}
}
}
// Pick solution[n][k] is the best solution to the problem.
}
}