-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackPack.java
More file actions
70 lines (48 loc) · 1.8 KB
/
BackPack.java
File metadata and controls
70 lines (48 loc) · 1.8 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
package leetcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class BackPack {
public static void main(String[] args) {
// backpack contains 4 kilo
// items has 3 items to be stolen
int bagSize = 4;
String[] items = {"guitar", "stereo", "laptdop"};
Integer[] weights = {1,4,3};
Integer[] values = {1500, 2000, 3000};
Integer[][] dp = new Integer[values.length+1][bagSize+1];
// HashMap<String,List<Integer>> hashMap = new HashMap<>();
// List<Integer> guitarSizeValue = new ArrayList<>();
// guitarSizeValue.add(values[0]);
// guitarSizeValue.add(size[0]);
//
// List<Integer> stereoSizeValue = new ArrayList<>();
// stereoSizeValue.add(values[1]);
// stereoSizeValue.add(size[1]);
//
// List<Integer> laptopSizeValue = new ArrayList<>();
// laptopSizeValue.add(values[2]);
// laptopSizeValue.add(size[2]);
// hashMap.put(items[0], guitarSizeValue);
// hashMap.put(items[1], stereoSizeValue);
// hashMap.put(items[2], laptopSizeValue);
//
// System.out.println(hashMap.get("guitar"));
// items = row , bagSize = column
for (int row = 0; row < dp.length; row++) {
for (int col = 0; col < dp[0].length; col++) {
if (col==0){
dp[row][col] = 0;
}else {
dp[row][col] = 1;
}
}
}
for (int row = 0; row < dp.length; row++) {
for (int col = 0; col < dp[0].length; col++) {
dp[row][col] = Math.max(dp[row-1][col-1],values[row]);
}
}
System.out.printf(" ");
}
}