-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaggage.java
More file actions
40 lines (36 loc) · 1.29 KB
/
baggage.java
File metadata and controls
40 lines (36 loc) · 1.29 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
import java.util.Arrays;
/**
* Author : WindAsMe
* File : baggage.java
* Time : Create on 18-8-14
* Location : ../Home/JavaForLeeCode2/baggage.java
* Function : 0/1 bag problem
*/
public class baggage {
private static int bag(int[] w, int[] v, int C) {
// The array is [Object's length + 1][Capacity + 1]
// Every step array[i][j] means:
// The Capacity is j, and you face the w[i], v[i] Object
int[][] ans = new int[w.length + 1][C + 1];
for (int i = 1; i <= w.length; i++) {
for (int j = 1; j <= C; j++) {
// If you're lack of Capacity
if (j < w[i - 1])
ans[i][j] = ans[i - 1][j];
// If you're spacious
else
// Comparing with the head or carry out the before
// You can do this because every step is OPTIMUM
ans[i][j] = Math.max(ans[i - 1][j], ans[i - 1][j - w[i - 1]] + v[i - 1]);
}
}
for (int[] a : ans)
System.out.println(Arrays.toString(a));
return ans[w.length][C];
}
public static void main(String[] args) {
int[] v = {2, 5, 3, 10, 4};
int[] w = {1, 3, 2, 6, 2};
System.out.println(bag(w, v, 12));
}
}