1
+ // Question:
2
+
3
+ // 0 - 1 Knapsack Problem :
4
+ // You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
5
+ // Note that we have only one quantity of each item.
6
+
7
+ // Example 1:
8
+
9
+ // Input:
10
+ // N = 3
11
+ // W = 4
12
+ // values[] = {1,2,3}
13
+ // weight[] = {4,5,1}
14
+ // Output: 3
15
+ // Example 2:
16
+
17
+ // Input:
18
+ // N = 3
19
+ // W = 3
20
+ // values[] = {1,2,3}
21
+ // weight[] = {4,5,6}
22
+ // Output: 0
23
+
24
+ // Approch followed: memoization
25
+
26
+
27
+ // Memoization: Make a choice digram for recursion,
28
+ // this will consist of if you can include the element in the knapsack or not,
29
+ // check if the weight of current item is less than or equal to toal weight,
30
+ // if it is, return the max of recursive call of the same function including
31
+ // the element (reduce the max Weight and n) or don;t include the element,
32
+ // this will oonly reduce n. And if the element's weight is more than current. don't include
33
+ // and call the function again with onoly n-1. for the DP part, initialize a global matrix and memset it to -1
34
+ // in the main code. whenever you are going in any condition, make the matrix on basis of the dimension of the varibale
35
+ // inputs of the recurisie function. Here it will be W and n, make a matrix of W+1 and n+1 dimension and for every recurvie
36
+ // return, instead, store that value insdie that matrix, before going into any call, check if
37
+ // the value foor that W and n is soomething else than the initalize mem set value of the matrix, if so directly return that
38
+
39
+
40
+
41
+ // { Driver Code Starts
42
+ #include < bits/stdc++.h>
43
+ using namespace std ;
44
+
45
+
46
+ // } Driver Code Ends
47
+
48
+
49
+
50
+ class Solution
51
+ {
52
+ public:
53
+
54
+ int t[1001 ][1001 ];
55
+
56
+ int solve (int W, int wt[], int val[], int n){
57
+ if (t[n][W]!=-1 ){
58
+ return t[n][W];
59
+ }
60
+ if (W==0 || n==0 ){
61
+ t[n][W]=0 ;
62
+ return 0 ;
63
+ }
64
+
65
+
66
+ if (wt[n-1 ]<=W){
67
+
68
+ t[n][W]=max (val[n-1 ]+solve (W-wt[n-1 ],wt,val,n-1 ),solve (W,wt,val,n-1 ));
69
+ return t[n][W];
70
+ }
71
+ else {
72
+ t[n][W]=solve (W,wt,val,n-1 );
73
+ return t[n][W];
74
+ }
75
+ }
76
+ // Function to return max value that can be put in knapsack of capacity W.
77
+ int knapSack (int W, int wt[], int val[], int n)
78
+ {
79
+
80
+ memset (t,-1 ,sizeof (t));
81
+
82
+ return solve (W,wt,val,n);
83
+ // Your code here
84
+
85
+ }
86
+ };
87
+
88
+ // { Driver Code Starts.
89
+
90
+ int main ()
91
+ {
92
+ // taking total testcases
93
+ int t;
94
+ cin>>t;
95
+ while (t--)
96
+ {
97
+ // reading number of elements and weight
98
+ int n, w;
99
+ cin>>n>>w;
100
+
101
+ int val[n];
102
+ int wt[n];
103
+
104
+ // inserting the values
105
+ for (int i=0 ;i<n;i++)
106
+ cin>>val[i];
107
+
108
+ // inserting the weights
109
+ for (int i=0 ;i<n;i++)
110
+ cin>>wt[i];
111
+ Solution ob;
112
+ // calling method knapSack()
113
+ cout<<ob.knapSack (w, wt, val, n)<<endl;
114
+
115
+ }
116
+ return 0 ;
117
+ } // } Driver Code Ends
0 commit comments