-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeeksforgeeks-01-knapsack-problem.cpp
More file actions
55 lines (46 loc) · 1.2 KB
/
Copy pathgeeksforgeeks-01-knapsack-problem.cpp
File metadata and controls
55 lines (46 loc) · 1.2 KB
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
// problem can be fount at: https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1#
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to return max value that can be put in knapsack of capacity W.
int knapSack(int capacity, int wt[], int val[], int numItems)
{
int cache[numItems+1][capacity+1];
int includeIth, excludeIth;
for(int i=0; i <= numItems; i++) {
for(int w=0; w <= capacity; w++) {
if (i == 0|| w == 0)
cache[i][w] = 0;
else if (wt[i-1] > w)
cache[i][w] = cache[i-1][w];
else {
includeIth = val[i-1] + cache[i-1][w - wt[i-1]] ;
excludeIth = cache[i-1][w];
cache[i][w] = max(includeIth, excludeIth);
}
}
}
return cache[numItems][capacity];
}
};
int main()
{
int cases;
cin >> cases;
while (cases--)
{
int numItems, capacity;
cin >> numItems >> capacity;
int val[numItems];
int wt[numItems];
for (int i = 0; i < numItems; i++)
cin >> val[i];
for (int i = 0; i < numItems; i++)
cin >> wt[i];
Solution obj;
cout << obj.knapSack(capacity, wt, val, numItems) << endl;
}
return 0;
}