-
Notifications
You must be signed in to change notification settings - Fork 13
/
knapsack.cpp
61 lines (51 loc) · 1.65 KB
/
knapsack.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
#include<iostream>
#include<vector>
using namespace std;
int solve(vector<int> &weight , vector<int> &value , int index , int capacity,vector<vector<int>> & dp){
if(capacity==0 || index>= value.size()){
return 0;
}
if (dp[index][capacity]!= -1){
return dp[index][capacity];
}
int include =0 ;
if(weight[index]<=capacity){
include = value[index] + solve(weight, value , index+1, capacity-weight[index],dp);
}
int exclude = solve(weight,value,index+1,capacity,dp);
return dp[index][capacity]= max(include,exclude);
}
int solveTab(vector<int> val, vector<int> wt, int C, int n) {
vector<vector<int> > dp(n+1, vector<int>(C+1, 0));
for(int index = n-1; index>=0; index--) {
for(int capacity =0; capacity <=C; capacity++) {
int include = 0;
if(capacity >= wt[index])
include = val[index] + dp[index+1][capacity-wt[index]];
int exclude = 0 + dp[index+1][capacity];
dp[index][capacity] = max(include, exclude);
}
}
/*for(int index = 0; index<=n; index++) {
for(int capacity =0; capacity <=C; capacity++) {
cout << dp[index][capacity] << " ";
}
cout << endl;
}*/
return dp[0][C];
}
int main(){
int n=4;
vector<int> weight ,value;
weight.push_back(1);
weight.push_back(2);
weight.push_back(4);
weight.push_back(5);
value.push_back(5);
value.push_back(4);
value.push_back(8);
value.push_back(6);
int capacity=5;
//vector<vector<int>> dp (n+1, vector<int>(capacity+1,-1));
cout<< solveTab(weight , value, capacity,n);
}