Given weights and values determine the maximum value that can be obtained in a knapsack of fixed capacity using DP. You are given: • An array weights[] • An array values[] • A knapsack with fixed capacity W Each item can be: • Taken once (1) • Or not taken (0) Your task is to find the maximum total value you can put in the knapsack without exceeding capacity.
weights = [1, 3, 4, 5] values = [1, 4, 5, 7] W = 7
9
You have a bag that can hold 7 weight units. You must choose items so that: • Total weight ≤ 7 • Total value is maximum Best choice: Take items with weight 3 (value 4) and weight 4 (value 5) Total weight = 7 Total value = 9 ✔ Brute Force Problem If we try all combinations: • Each item has 2 choices (take / not take) • Total combinations = 2ⁿ ❌ (very slow) DP Idea: Dynamic Programming means: “Solve small problems first and use them to solve bigger problems.” DP Table Meaning (Easy) Let: dp[i][w] Means: Maximum value using first i items with knapsack capacity w
Step-by-Step Solution (Recommended) 🔹 Step 1: Create DP Table dp[n+1][W+1] 🔹 Step 2: Base Case If no items or capacity is 0 → value = 0 🔹 Step 3: Fill the Table For each item: If item weight ≤ current capacity: Take max of: Not taking the item Taking the item Else: Skip the item 🔹 Step 4: Final Answer dp[n][W] DP Formula if (weights[i-1]
You have a bag that can hold 7 weight units. You must choose items so that: • Total weight ≤ 7 • Total value is maximum Best choice: Take items with weight 3 (value 4) and weight 4 (value 5) Total weight = 7 Total value = 9 ✔ Brute Force Problem If we try all combinations: • Each item has 2 choices (take / not take) • Total combinations = 2ⁿ ❌ (very slow) DP Idea: Dynamic Programming means: “Solve small problems first and use them to solve bigger problems.” DP Table Meaning (Easy) Let: dp[i][w] Means: Maximum value using first i items with knapsack capacity w
Step-by-Step Solution (Recommended) 🔹 Step 1: Create DP Table dp[n+1][W+1] 🔹 Step 2: Base Case If no items or capacity is 0 → value = 0 🔹 Step 3: Fill the Table For each item: If item weight ≤ current capacity: Take max of: Not taking the item Taking the item Else: Skip the item 🔹 Step 4: Final Answer dp[n][W] DP Formula if (weights[i-1]
#include <bits/stdc++.h>
using namespace std;
int knapsack(int W, vector<int>& wt, vector<int>& val) {
int n = wt.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 (wt[i - 1] <= w) {
dp[i][w] = max(
dp[i - 1][w],
val[i - 1] + dp[i - 1][w - wt[i - 1]]
);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
vector<int> wt = {1, 3, 4, 5};
vector<int> val = {1, 4, 5, 7};
int W = 7;
cout << knapsack(W, wt, val);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int knapsack(int W, vector<int>& wt, vector<int>& val) {
int n = wt.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 (wt[i - 1] <= w) {
dp[i][w] = max(
dp[i - 1][w],
val[i - 1] + dp[i - 1][w - wt[i - 1]]
);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
vector<int> wt = {1, 3, 4, 5};
vector<int> val = {1, 4, 5, 7};
int W = 7;
cout << knapsack(W, wt, val);
return 0;
}