-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08- Knapsack Problem.cpp
More file actions
40 lines (36 loc) · 1.37 KB
/
Copy path08- Knapsack Problem.cpp
File metadata and controls
40 lines (36 loc) · 1.37 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
// In The Name of Allah
// 0/1 Knapsack
//
// Problem: Given n items with weights wt[] and values val[] and a knapsack
// of capacity W, choose a subset of items to maximize total value
// without exceeding W. Each item is either taken once or not at all.
// Approach: dp[i][w] = max value using the first i items with capacity w.
// dp[i][w] = dp[i-1][w] (skip)
// = max(dp[i-1][w], val[i-1] + dp[i-1][w - wt[i-1]])
// if wt[i-1] <= w (take)
// Time: O(n * W)
// Space: O(n * W), reducible to O(W) with a 1-D rolling array.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int knapsack(int W, const vector<int>& wt, const vector<int>& val) {
int n = (int)wt.size();
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= W; w++) {
dp[i][w] = dp[i - 1][w];
if (wt[i - 1] <= w) {
dp[i][w] = max(dp[i][w], val[i - 1] + dp[i - 1][w - wt[i - 1]]);
}
}
}
return dp[n][W];
}
int main() {
vector<int> val = {60, 100, 120};
vector<int> wt = {10, 20, 30};
int W = 50;
cout << "Best value with capacity " << W << ": " << knapsack(W, wt, val) << endl;
return 0;
}