1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ // printing dp table
5
+ void print (vector<vector<int >> &dp)
6
+ {
7
+ for (auto row : dp)
8
+ {
9
+ for (auto col : row)
10
+ {
11
+ cout << col << " " ;
12
+ }
13
+ cout << endl;
14
+ }
15
+ }
16
+
17
+ // tc: O(2^n) sc: O(n)
18
+ // int solvesingRecursion(int capacity, vector<int> &wt, vector<int> &profit, int i, int &n){
19
+ // // base case
20
+ // if(i >= n){
21
+ // return 0;
22
+ // }
23
+
24
+ // // inc/exc
25
+ // int inc = 0;
26
+ // if(wt[i] <= capacity){
27
+ // inc = profit[i] + solvesingRecursion(capacity-wt[i], wt, profit, i+1, n);
28
+ // }
29
+ // int exc = 0 + solvesingRecursion(capacity, wt, profit, i+1, n);
30
+ // return max(inc, exc);
31
+ // }
32
+
33
+ // tc: O(n*capacity) sc: O(n*capacity)
34
+ // int solvesingMemo(int capacity, vector<int> &wt, vector<int> &profit, int i, int &n, vector<vector<int>> &dp)
35
+ // {
36
+ // // base case
37
+ // if (i >= n)
38
+ // {
39
+ // return 0;
40
+ // }
41
+
42
+ // // check if already calculated
43
+ // if (dp[capacity][i] != -1)
44
+ // {
45
+ // return dp[capacity][i];
46
+ // }
47
+
48
+ // // inc/exc
49
+ // int inc = 0;
50
+ // if (wt[i] <= capacity)
51
+ // {
52
+ // inc = profit[i] + solvesingMemo(capacity - wt[i], wt, profit, i + 1, n, dp);
53
+ // }
54
+ // int exc = 0 + solvesingMemo(capacity, wt, profit, i + 1, n, dp);
55
+ // dp[capacity][i] = max(inc, exc);
56
+ // return dp[capacity][i];
57
+ // }
58
+
59
+ // tc: O(n*capacity) sc: O(n*capacity)
60
+ // int solveUsingTabu(int capacity, vector<int> &wt, vector<int> &profit, vector<vector<int>> &dp, int &n)
61
+ // {
62
+ // for (int row = 0; row <= capacity; row++)
63
+ // {
64
+ // dp[row][n] = 0;
65
+ // }
66
+
67
+ // for (int i = 0; i <= capacity; i++)
68
+ // {
69
+ // for (int j = n-1; j >= 0; j--)
70
+ // {
71
+ // // inc/exc
72
+ // int inc = 0;
73
+ // if (wt[j] <= i)
74
+ // {
75
+ // inc = profit[j] + dp[i - wt[j]][j + 1];
76
+ // }
77
+ // int exc = 0 + dp[i][j + 1];
78
+ // dp[i][j] = max(inc, exc);
79
+ // }
80
+ // }
81
+ // return dp[capacity][0];
82
+ // }
83
+
84
+ // tc: O(n*capacity) sc: O(capacity)
85
+ // int solveUsingTabuSpaceOpti(int capacity, vector<int> &wt, vector<int> &profit, int &n)
86
+ // {
87
+ // vector<int> nextCol(capacity + 1, 0);
88
+ // vector<int> currCol(capacity + 1, -1);
89
+
90
+ // for (int j = n - 1; j >= 0; j--)
91
+ // {
92
+ // for (int i = 0; i <= capacity; i++)
93
+ // {
94
+ // // inc/exc
95
+ // int inc = 0;
96
+ // if (wt[j] <= i)
97
+ // {
98
+ // inc = profit[j] + nextCol[i - wt[j]];
99
+ // }
100
+ // int exc = 0 + nextCol[i];
101
+ // currCol[i] = max(inc, exc);
102
+ // }
103
+ // nextCol = currCol;
104
+ // }
105
+ // return currCol[capacity];
106
+ // }
107
+
108
+ // tc: O(n*capacity) sc: O(capacity)
109
+ int solveUsingTabuSpaceOpti2 (int capacity, vector<int > &wt, vector<int > &profit, int &n)
110
+ {
111
+ vector<int > nextCol (capacity + 1 , 0 );
112
+ // vector<int> currCol(capacity + 1, -1);
113
+
114
+ for (int j = n - 1 ; j >= 0 ; j--)
115
+ {
116
+ for (int i = capacity; i >= 0 ; i--)
117
+ {
118
+ // inc/exc
119
+ int inc = 0 ;
120
+ if (wt[j] <= i)
121
+ {
122
+ inc = profit[j] + nextCol[i - wt[j]];
123
+ }
124
+ int exc = 0 + nextCol[i];
125
+ nextCol[i] = max (inc, exc);
126
+ }
127
+
128
+ }
129
+ return nextCol[capacity];
130
+ }
131
+
132
+ int knapsack (vector<int > weight, vector<int > profit, int n, int capacity)
133
+ {
134
+ int i = 0 ;
135
+ // return solvesingRecursion(capacity, weight, profit, i, n);
136
+ vector<vector<int >> dp (capacity + 1 , vector<int >(n + 1 , -1 ));
137
+ // return solveUsingMemo(capacity, weight, profit, i, n, dp);
138
+ // int ans = solveUsingTabu(capacity, weight, profit, dp, n);
139
+ // int ans = solveUsingTabuSpaceOpti(capacity, weight, profit, n);
140
+ int ans = solveUsingTabuSpaceOpti2 (capacity, weight, profit, n);
141
+ cout << " DP table:" << endl;
142
+ print (dp);
143
+ cout << endl;
144
+ cout << " Max profit:" << endl;
145
+ return ans;
146
+ }
147
+
148
+ int main ()
149
+ {
150
+ vector<int > weight = {1 , 2 , 3 };
151
+ vector<int > profit = {10 , 15 , 40 };
152
+ int n = weight.size ();
153
+ int capacity = 6 ;
154
+ cout << knapsack (weight, profit, n, capacity) << endl;
155
+ return 0 ;
156
+ }
0 commit comments