Skip to content

Commit 5d7522e

Browse files
committed
backpack
1 parent 12b55f6 commit 5d7522e

3 files changed

Lines changed: 770 additions & 653 deletions

File tree

‎Java/Backpack.java‎

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
M
2+
1517903196
23

3-
DP。
4+
需要从'可能性'的角度考虑, 不要搞成单一的最大值问题.
5+
6+
1. 背包大小和总承重有关.
7+
2. 不要去找f[i]前i个物品的最大总重, 找的不是这个.
8+
f[i]及时找到可放的最大sum, 但是i+1可能有更好的值, 把f[i+1]变得更大更合适.
9+
10+
DP.
411
row是item大小: 0, A[0], A[1] ... A[A.length -1]
512
col是背包累积的size: 0, 1, 2, ... m.
613

@@ -49,6 +56,110 @@
4956
5057
5158
*/
59+
/*
60+
Thoughts:
61+
DO NOT try to find the maxSum using given values: this approach f[i] only returns max sum,
62+
but does not guarantee to pick the most sutible items that maximize f[n].
63+
64+
1. Always consider weight
65+
2. Consider it as a possibility problem based on weight 0 ~ m
66+
67+
Using given items, can we fill backpack with size 0, size 1, size 2... size m - 1, and size m?
68+
We save these values with boolean dp[i][w]: using i items to fill size w, where i is the A index.
69+
70+
Two conditions for calculating dp[i][w]
71+
1. i - 1 items filled up w, so dp[i][w] = dp[i - 1][w]. No need to add A[i - 1].
72+
2. i - 1 items fileld up w - A[i - 1]; once adding A[i - 1], it'll fill up w. dp[i][w] = dp[i - 1][w - A[i - 1]], so we are counting on if i - 1 items filled up weight: w - A[i - 1].
73+
74+
We'll loop over j = 1 ~ m, and attempt to fill all sizes with i items.
75+
76+
init:
77+
dp[0][0]: using 0 items to fill 0 space, sure. True.
78+
dp[0][1~m]: using 0 items to fill 1~m space, not gonna work. False.
79+
80+
81+
*/
82+
public class Solution {
83+
84+
public int backPack(int m, int[] A) {
85+
if (A == null || A.length < 0) {
86+
return 0;
87+
}
88+
int n = A.length;
89+
boolean[][] dp = new boolean[n + 1][m + 1];
90+
91+
// weight 0 is a valid value.
92+
// items does not have 0's item, so we need to init dp based for all entries where i == 0
93+
dp[0][0] = true;
94+
for (int j = 1; j <= m; j++) {
95+
dp[0][j] = false;
96+
}
97+
98+
// Calculcate possibility for i items to fill up w weight
99+
for (int i = 1; i <= n; i++) {
100+
for (int j = 0; j <= m; j++) {
101+
// default: item(i-1) not used:
102+
dp[i][j] = dp[i - 1][j];
103+
if (j - A[i - 1] >= 0) { // possible to use item(i-1)
104+
dp[i][j] |= dp[i - 1][j - A[i - 1]]; // use item(i-1)
105+
}
106+
}
107+
}
108+
109+
// Find max weight size that makes dp[i][j] true
110+
for (int j = m; j >= 0; j--) {
111+
if (dp[n][j]) {
112+
return j;
113+
}
114+
}
115+
return 0;
116+
}
117+
}
118+
119+
/*
120+
Thoughts: rolling array.
121+
Always use i, and i - 1 dp. can replace the two index with
122+
curr, pre
123+
*/
124+
public class Solution {
125+
126+
public int backPack(int m, int[] A) {
127+
if (A == null || A.length < 0) {
128+
return 0;
129+
}
130+
int n = A.length;
131+
boolean[][] dp = new boolean[2][m + 1];
132+
int curr = 0;
133+
int pre = 1;
134+
// weight 0 is a valid value.
135+
// items does not have 0's item, so we need to init dp based for all entries where i == 0
136+
dp[curr][0] = true;
137+
for (int j = 1; j <= m; j++) {
138+
dp[curr][j] = false;
139+
}
140+
141+
// Calculcate possibility for i items to fill up w weight
142+
for (int i = 1; i <= n; i++) {
143+
curr = pre;
144+
pre = 1 - curr;
145+
for (int j = 0; j <= m; j++) {
146+
// default: item(i-1) not used:
147+
dp[curr][j] = dp[pre][j];
148+
if (j - A[i - 1] >= 0) { // possible to use item(i-1)
149+
dp[curr][j] |= dp[pre][j - A[i - 1]]; // use item(i-1)
150+
}
151+
}
152+
}
153+
154+
// Find max weight size that makes dp[i][j] true
155+
for (int j = m; j >= 0; j--) {
156+
if (dp[curr][j]) {
157+
return j;
158+
}
159+
}
160+
return 0;
161+
}
162+
}
52163

53164
/*
54165
Thoughts: Recap on 12.02.2015

0 commit comments

Comments
 (0)