-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathknapsack.cpp
More file actions
305 lines (278 loc) · 9.47 KB
/
knapsack.cpp
File metadata and controls
305 lines (278 loc) · 9.47 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Knapsack Problems
Mathematical Foundation: Choose items to maximize value within weight constraint
0/1 Knapsack: dp[i][w] = max(dp[i-1][w], dp[i-1][w-weight[i]] + value[i])
Unbounded: Can use item multiple times
Time: O(n*W), Space: O(W) with optimization
*/
#include <bits/stdc++.h>
using namespace std;
// 0/1 Knapsack - 2D DP (Classic Pattern)
// Related LeetCode Problems:
// 416. Partition Equal Subset Sum
// https://leetcode.com/problems/partition-equal-subset-sum/
// 494. Target Sum
// https://leetcode.com/problems/target-sum/
// 474. Ones and Zeroes
// https://leetcode.com/problems/ones-and-zeroes/
// 1049. Last Stone Weight II
// https://leetcode.com/problems/last-stone-weight-ii/
int knapsack01_2D(vector<int>& weights, vector<int>& values, int W) {
int n = weights.size();
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (weights[i - 1] <= w) {
dp[i][w] = max(dp[i - 1][w],
dp[i - 1][w - weights[i - 1]] + values[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
// 0/1 Knapsack - Space Optimized
// Related LeetCode Problems:
// 416. Partition Equal Subset Sum
// https://leetcode.com/problems/partition-equal-subset-sum/
// 494. Target Sum
// https://leetcode.com/problems/target-sum/
// 1049. Last Stone Weight II
// https://leetcode.com/problems/last-stone-weight-ii/
// 698. Partition to K Equal Sum Subsets
// https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
int knapsack01(vector<int>& weights, vector<int>& values, int W) {
vector<int> dp(W + 1, 0);
for (int i = 0; i < weights.size(); i++) {
for (int w = W; w >= weights[i]; w--) {
dp[w] = max(dp[w], dp[w - weights[i]] + values[i]);
}
}
return dp[W];
}
// Unbounded Knapsack
// Related LeetCode Problems:
// 322. Coin Change
// https://leetcode.com/problems/coin-change/
// 518. Coin Change 2
// https://leetcode.com/problems/coin-change-2/
// 377. Combination Sum IV
// https://leetcode.com/problems/combination-sum-iv/
// 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// 983. Minimum Cost For Tickets
// https://leetcode.com/problems/minimum-cost-for-tickets/
int unboundedKnapsack(vector<int>& weights, vector<int>& values, int W) {
vector<int> dp(W + 1, 0);
for (int w = 1; w <= W; w++) {
for (int i = 0; i < weights.size(); i++) {
if (weights[i] <= w) {
dp[w] = max(dp[w], dp[w - weights[i]] + values[i]);
}
}
}
return dp[W];
}
// Coin Change (Minimum Coins)
// LeetCode: 322. Coin Change
// https://leetcode.com/problems/coin-change/
// Related:
// 518. Coin Change 2
// https://leetcode.com/problems/coin-change-2/
// 377. Combination Sum IV
// https://leetcode.com/problems/combination-sum-iv/
// 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// 983. Minimum Cost For Tickets
// https://leetcode.com/problems/minimum-cost-for-tickets/
// 1449. Form Largest Integer With Digits That Add up to Target
// https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (i >= coin) {
dp[i] = min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
// Coin Change II (Number of Ways)
// LeetCode: 518. Coin Change 2
// https://leetcode.com/problems/coin-change-2/
// Related:
// 322. Coin Change
// https://leetcode.com/problems/coin-change/
// 377. Combination Sum IV
// https://leetcode.com/problems/combination-sum-iv/
// 39. Combination Sum
// https://leetcode.com/problems/combination-sum/
// 40. Combination Sum II
// https://leetcode.com/problems/combination-sum-ii/
// 216. Combination Sum III
// https://leetcode.com/problems/combination-sum-iii/
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1, 0);
dp[0] = 1;
for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
// Partition Equal Subset Sum
// LeetCode: 416. Partition Equal Subset Sum
// https://leetcode.com/problems/partition-equal-subset-sum/
// Related:
// 494. Target Sum
// https://leetcode.com/problems/target-sum/
// 1049. Last Stone Weight II
// https://leetcode.com/problems/last-stone-weight-ii/
// 698. Partition to K Equal Sum Subsets
// https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
// 473. Matchsticks to Square
// https://leetcode.com/problems/matchsticks-to-square/
bool canPartition(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % 2) return false;
int target = sum / 2;
vector<bool> dp(target + 1, false);
dp[0] = true;
for (int num : nums) {
for (int j = target; j >= num; j--) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
// Target Sum
// LeetCode: 494. Target Sum
// https://leetcode.com/problems/target-sum/
// Related:
// 416. Partition Equal Subset Sum
// https://leetcode.com/problems/partition-equal-subset-sum/
// 1049. Last Stone Weight II
// https://leetcode.com/problems/last-stone-weight-ii/
// 698. Partition to K Equal Sum Subsets
// https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
// 2035. Partition Array Into Two Arrays to Minimize Sum Difference
// https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/
int findTargetSumWays(vector<int>& nums, int target) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum < abs(target) || (sum + target) % 2) return 0;
int subset_sum = (sum + target) / 2;
vector<int> dp(subset_sum + 1, 0);
dp[0] = 1;
for (int num : nums) {
for (int j = subset_sum; j >= num; j--) {
dp[j] += dp[j - num];
}
}
return dp[subset_sum];
}
// Ones and Zeroes
// LeetCode: 474. Ones and Zeroes
// https://leetcode.com/problems/ones-and-zeroes/
// Related:
// 416. Partition Equal Subset Sum
// https://leetcode.com/problems/partition-equal-subset-sum/
// 494. Target Sum
// https://leetcode.com/problems/target-sum/
// 1049. Last Stone Weight II
// https://leetcode.com/problems/last-stone-weight-ii/
// 600. Non-negative Integers without Consecutive Ones
// https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (string& s : strs) {
int zeros = count(s.begin(), s.end(), '0');
int ones = s.size() - zeros;
for (int i = m; i >= zeros; i--) {
for (int j = n; j >= ones; j--) {
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
}
// Last Stone Weight II
// LeetCode: 1049. Last Stone Weight II
// https://leetcode.com/problems/last-stone-weight-ii/
// Related:
// 416. Partition Equal Subset Sum
// https://leetcode.com/problems/partition-equal-subset-sum/
// 494. Target Sum
// https://leetcode.com/problems/target-sum/
// 1046. Last Stone Weight
// https://leetcode.com/problems/last-stone-weight/
// 2035. Partition Array Into Two Arrays to Minimize Sum Difference
// https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/
int lastStoneWeightII(vector<int>& stones) {
int sum = accumulate(stones.begin(), stones.end(), 0);
int target = sum / 2;
vector<bool> dp(target + 1, false);
dp[0] = true;
for (int stone : stones) {
for (int j = target; j >= stone; j--) {
dp[j] = dp[j] || dp[j - stone];
}
}
for (int i = target; i >= 0; i--) {
if (dp[i]) return sum - 2 * i;
}
return 0;
}
// Perfect Squares
// LeetCode: 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// Related:
// 322. Coin Change
// https://leetcode.com/problems/coin-change/
// 264. Ugly Number II
// https://leetcode.com/problems/ugly-number-ii/
// 204. Count Primes
// https://leetcode.com/problems/count-primes/
// 343. Integer Break
// https://leetcode.com/problems/integer-break/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
int numSquares(int n) {
vector<int> dp(n + 1, n);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j * j <= i; j++) {
dp[i] = min(dp[i], dp[i - j * j] + 1);
}
}
return dp[n];
}
// Combination Sum IV
// LeetCode: 377. Combination Sum IV
// https://leetcode.com/problems/combination-sum-iv/
// Related:
// 39. Combination Sum
// https://leetcode.com/problems/combination-sum/
// 40. Combination Sum II
// https://leetcode.com/problems/combination-sum-ii/
// 216. Combination Sum III
// https://leetcode.com/problems/combination-sum-iii/
// 518. Coin Change 2
// https://leetcode.com/problems/coin-change-2/
// 70. Climbing Stairs
// https://leetcode.com/problems/climbing-stairs/
int combinationSum4(vector<int>& nums, int target) {
vector<unsigned int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int num : nums) {
if (i >= num) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}