-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_01KnapsackDuplicateItems.cpp
86 lines (71 loc) · 1.98 KB
/
GFG_01KnapsackDuplicateItems.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
https://practice.geeksforgeeks.org/problems/knapsack-with-duplicate-items4201/1#
Knapsack with duplicate items
*/
// { Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution{
public:
int knapSack(int N, int W, int val[], int wt[])
{
// vector<vector<int>> dp(N+1, vector<int>(W+1, -1));
// return knapSackRec(N, W, dp, wt, val);
// int dp1[W+1] = {0};
// // for(int w=W; w>=0; w--)
// for(int w=1; w<=W; w++)
// {
// for(int i=0; i<N; i++)
// {
// if(wt[i] <= w)
// dp1[w] = max(dp1[w], val[i]+dp1[w-wt[i]]);
// cout<<dp1[w]<<" ";
// }
// }
// cout<<endl;
int dp[W+1] = {0};
for(int i=0; i<N; i++)
{
for(int w=wt[i]; w<=W; w++)
{
// if(wt[i] <= w)
dp[w] = max(dp[w], val[i]+dp[w-wt[i]]);
// cout<<dp[w]<<" ";
}
}
return dp[W];
}
int knapSackRec(int idx, int W, vector<vector<int>>& dp, int *wt, int *val)
{
if(idx <= 0)
return 0;
if(W == 0)
return 0;
if(dp[idx][W] != -1)
return dp[idx][W];
if(wt[idx-1] > W)
return dp[idx][W] = knapSackRec(idx-1, W, dp, wt, val);
else
return dp[idx][W] = max(knapSackRec(idx-1, W, dp, wt, val), knapSackRec(idx, W-wt[idx-1], dp, wt, val)+val[idx-1]);
}
};
// { Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int N, W;
cin>>N>>W;
int val[N], wt[N];
for(int i = 0;i < N;i++)
cin>>val[i];
for(int i = 0;i < N;i++)
cin>>wt[i];
Solution ob;
cout<<ob.knapSack(N, W, val, wt)<<endl;
}
return 0;
} // } Driver Code Ends