forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01knapsack(A.V).cpp
124 lines (104 loc) · 2.57 KB
/
01knapsack(A.V).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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
using namespace std;
int max(int a, int b)
{
return (a > b) ? a : b;
}
int knapsack(int wt[], int val[], int W, int N, int** tb)
{
if(N<=0 || W==0)
{
return 0;
}
if(tb[N][W]!=-1)
{ //Memoization
return tb[N][W];
}
if(wt[N-1]<=W)
{
return tb[N][W]=max(val[N-1] + knapsack(wt, val, W-wt[N-1],N-1, tb), knapsack(wt, val, W, N-1, tb));
}
else if(wt[N-1]>W)
{
return tb[N][W]=knapsack(wt, val, W, N-1, tb);
}
}
int knapSackR(int W, int wt[], int val[], int n)
{
// double pointer to declare the
// table dynamically
int** dp;
dp = new int*[n];
// loop to create the table dynamically
for (int i = 0; i < n; i++)
dp[i] = new int[W + 1];
// loop to initially filled the
// table with -1
for (int i = 0; i < n; i++)
for (int j = 0; j < W + 1; j++)
dp[i][j] = -1;
return knapsack(wt, val, W, n, dp);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,N,W,x;
cin>>t; //t=[1,100]
while(t--)
{
cin>>N; //N=[1,1000]
cin>>W; //W=[1,1000]
int wt[W]; //wt[i]=[1,1000]
int val[N]; //val[i]=[1,1000]
for (int i = 0; i <N; i++)
{
/* code */
cin>>x;
val[i]=x;
}
for (int i = 0; i <W; i++)
{
/* code */
cin>>x;
wt[i]=x;
}
int n = sizeof(val) / sizeof(val[0]);
cout << knapSackR(W, wt, val, n);
}
}
//top down approach
#include <iostream>
using namespace std;
void knap(int n, int w, int val[],int weight[])
{
int t[n+1][w+1];
//fill 0th rows and coloumns with zero.
for(int i=0;i<=n;i++)
t[i][0]=0;
for(int j=0;j<=w;j++)
t[0][j]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=w;j++)
{
if(j<weight[i])
t[i][j]=t[i-1][j];
else
t[i][j]=max((val[i]+t[i-1][j-weight[i]]),t[i-1][j]);
}
}
cout<<t[n][w]<<endl; //last element
}
int main() {int y; cin>>y;
while(y--)
{ int n; cin>>n;
int w; cin>>w;
int val[n];int weight[n];
for(int i=1;i<=n;i++)
cin>>val[i];
for(int i=1;i<=n;i++)
cin>>weight[i];
knap(n,w,val,weight);
}
return 0;
}