-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackpack.cpp
More file actions
36 lines (33 loc) · 1022 Bytes
/
Copy pathbackpack.cpp
File metadata and controls
36 lines (33 loc) · 1022 Bytes
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
class Solution {
public:
/**
* @param m: An integer m denotes the size of a backpack
* @param A: Given n items with size A[i]
* @return: The maximum size
*/
// the space complex can be optimized.
//
int backPack(int m, vector<int> A) {
// write your code here
if (A.empty() || m <= 0)
return -1;
vector<vector<bool>> f(A.size() + 1, vector<bool> (m + 1, false));
//f[i][j] first i elements and backpack size j
for (int i = 0; i < A.size() + 1; i++) {
f[i][0] = true;
}
for (int i = 1; i <= A.size(); i++) {
for (int j = 1; j <= m; j++) {
bool temp = f[i - 1][j];
if (j - A[i - 1] >= 0)
temp = temp || f[i-1][j - A[i - 1]];
f[i][j] = temp;
}
}
for (int i = m; m >=0; i--) {
if (f[A.size()][i])
return i;
}
return -1;
}
};