-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack-unbounded.cpp
66 lines (59 loc) · 1.72 KB
/
knapsack-unbounded.cpp
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
#include <iostream>
#include <cstring>
using namespace std;
int recurse(int length, int limit, int weights[], int values[], int dp[]) {
if(dp[limit] != -1) {
return dp[limit];
}
int m = 0;
for (size_t idx = 0; idx < length; idx++) {
int taken = weights[idx];
if(taken <= limit) {
m = max(values[idx] + recurse(length, limit - taken, weights, values, dp), m);
}
}
dp[limit] = m;
return dp[limit];
}
int main() {
int weights[] = {5, 10, 15};
int values[] = {10, 30, 20};
int limit = 100;
int n = sizeof(weights) / sizeof(weights[0]);
int dp[limit + 1];
memset(dp, -1, sizeof(dp));
cout << recurse(n, limit, weights, values, dp);
return 0;
}
// #include <iostream>
// #include <cstring>
// using namespace std;
// int a = 0;
// int recurse(int idx, int limit, int weights[], int values[], int dp[][101]) {
// if(idx == -1 || limit == 0) {
// return 0;
// }
// if(dp[idx][limit] != -1) {
// return dp[idx][limit];
// }
// a++;
// int taken = weights[idx];
// if(taken > limit) {
// return dp[idx][limit] = recurse(idx - 1, limit, weights, values, dp);
// }
// return dp[idx][limit] = m(values[idx] + recurse(idx, limit - taken, weights, values, dp), recurse(idx - 1, limit, weights, values, dp));
// }
// int main() {
// int weights[] = {5, 10, 15};
// int values[] = {10, 30, 20};
// int limit = 100;
// int n = sizeof(weights) / sizeof(weights[0]);
// int dp[n][101];
// memset(dp, -1, sizeof(dp));
// cout << recurse(n - 1, limit, weights, values, dp);
// // cout << " " << a;
// // for (size_t i = 0; i < limit + 1; i++) {
// // cout << dp[i] << ' ';
// // }
// return 0;
// }