-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathknapsack_problem.cpp
41 lines (26 loc) · 960 Bytes
/
knapsack_problem.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
#include <bits/stdc++.h>
using namespace std;
int knapsackUtil(vector<int>& wt, vector<int>& val, int ind, int W, vector<vector<int>>& dp){
if(ind == 0){
if(wt[0] <=W) return val[0];
else return 0;
}
if(dp[ind][W]!=-1)
return dp[ind][W];
int notTaken = 0 + knapsackUtil(wt,val,ind-1,W,dp);
int taken = INT_MIN;
if(wt[ind] <= W)
taken = val[ind] + knapsackUtil(wt,val,ind-1,W-wt[ind],dp);
return dp[ind][W] = max(notTaken,taken);
}
int knapsack(vector<int>& wt, vector<int>& val, int n, int W){
vector<vector<int>> dp(n,vector<int>(W+1,-1));
return knapsackUtil(wt, val, n-1, W, dp);
}
int main() {
vector<int> wt = {1,2,4,5};
vector<int> val = {5,4,8,6};
int W=5;
int n = wt.size();
cout<<"The Maximum value of items, thief can steal is " <<knapsack(wt,val,n,W);
}