-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22_KnapSack.cpp
179 lines (162 loc) · 4.67 KB
/
22_KnapSack.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// https://www.codingninjas.com/codestudio/problems/0-1-knapsack_920542
// A thief is robbing a store and can carry a maximal weight of W into his
// knapsack. There are N items and the ith item weighs wi and is of value vi.
// Considering the constraints of the maximum weight that a knapsack can carry,
// you have to find and return the maximum value that a thief can generate by stealing items.
#include <bits/stdc++.h>
using namespace std;
class Solution4
{
// Tabulation: Space Optimised to single rowDp
public:
int knapsack(vector<int> &weight, vector<int> &value, int &n, int &maxWeight)
{
// Write your code here
vector<int> cp(maxWeight+1);
for (int i = 0; i <= maxWeight; ++i)
{
if(weight[0] <= i) cp[i]= value[0];
}
for (int i = 1; i < n; ++i)
{
vector<int> temp(maxWeight+1);
for (int j = maxWeight; j >= 0; --j)
{
int pick = INT_MIN;
if (weight[i] <= j)
{
pick = value[i] + cp[j-weight[i]];
}
// NotPick
int noPick = cp[j];
cp[j] = max(pick, noPick) ;
}
}
return cp[maxWeight];
}
};
class Solution3
{
// Tabulation: Space Optimised
public:
int knapsack(vector<int> &weight, vector<int> &value, int &n, int &maxWeight)
{
// Write your code here
vector<int> cp(maxWeight+1);
for (int i = 0; i <= maxWeight; ++i)
{
if(weight[0] <= i) cp[i]= value[0];
}
for (int i = 1; i < n; ++i)
{
vector<int> temp(maxWeight+1);
for (int j = 0; j <= maxWeight; ++j)
{
int pick = INT_MIN;
if (weight[i] <= j)
{
pick = value[i] + cp[j-weight[i]];
}
// NotPick
int noPick = cp[j];
temp[j] = max(pick, noPick) ;
}
cp = temp ;
}
return cp[maxWeight];
}
};
class Solution2
{
// Tabulation
public:
int knapsack(vector<int> &weight, vector<int> &value, int &n, int &maxWeight)
{
// Write your code here
vector<vector<int>> dp(n, vector<int>(maxWeight + 1));
for (int i = 0; i <= maxWeight; ++i)
{
if(weight[0] <= i) dp[0][i] = value[0];
}
for (int i = 1; i < n; ++i)
{
for (int j = 0; j <= maxWeight; ++j)
{
int pick = INT_MIN;
if (weight[i] <= j)
{
pick = value[i] + dp[i-1][j-weight[i]];
}
// NotPick
int noPick = dp[i-1][j];
dp[i][j] = max(pick, noPick) ;
}
}
return dp[n-1][maxWeight];
}
};
class Solution1
{
// Recursion : Memoization
public:
int knapsack(vector<int> &weight, vector<int> &value, int &n, int &maxWeight)
{
// Write your code here
vector<vector<int>> dp(n, vector<int>(maxWeight + 1, -1));
return steal(weight, value, dp, maxWeight);
}
int steal(vector<int> &weight, vector<int> &value, vector<vector<int>> &dp, int maxWeight, int i = 0)
{
if (i == weight.size())
{
return 0;
}
if (dp[i][maxWeight] != -1)
return dp[i][maxWeight];
// Pick
int pick = INT_MIN;
if (weight[i] <= maxWeight)
{
pick = value[i] + steal(weight, value, dp, maxWeight - weight[i], i + 1);
}
// NotPick
int noPick = steal(weight, value, dp, maxWeight, i + 1);
return dp[i][maxWeight] = max(pick, noPick);
}
};
class Solution
{
// BruteForce: Recursion
public:
int knapsack(vector<int> &weight, vector<int> &value, int &n, int &maxWeight)
{
// Write your code here
return steal(weight, value, maxWeight);
}
int steal(vector<int> &weight, vector<int> &value, int maxWeight, int i = 0)
{
if (i == weight.size())
{
return 0;
}
// Pick
int pick = INT_MIN;
if (weight[i] <= maxWeight)
{
pick = value[i] + steal(weight, value, maxWeight - weight[i], i + 1);
}
// NotPick
int noPick = steal(weight, value, maxWeight, i + 1);
return max(pick, noPick);
}
};
int main()
{
int n = 4, maxWeight = 5;
vector<int> weight = {1, 2, 4, 5}, value = {5, 4, 8, 6};
Solution4 Obj1;
cout << Obj1.knapsack(weight, value, n, maxWeight);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}