You are given weights and values of items, and put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
In other words, given two integer arrays val and wt which represent values and weights associated with items respectively. Also given an integer W which represents knapsack capacity, find out the maximum sum values subset of val[] such that the sum of the weights of the corresponding subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).
Examples :
Input: W = 4, val[] = {1,2,3}, wt[] = {4,5,1}
Output: 3
Explanation: Choose the last item that weighs 1 unit and holds a value of 3.
Input: W = 3, val[] = {1,2,3}, wt[] = {4,5,6}
Output: 0
Explanation: Every item has a weight exceeding the knapsack's capacity (3).
Expected Time Complexity: O(N*W).
Expected Auxiliary Space: O(N*W)
Constraints:
2 ≤ N ≤ 1000
1 ≤ W ≤ 1000
1 ≤ wt[i] ≤ 1000
1 ≤ val[i] ≤ 1000
Approach 01:
-
C++
-
Python
#include <vector>
#include <algorithm>
#include <iostream>
class Solution {
public:
int knapSack(int maxWeight, const std::vector<int>& weights, const std::vector<int>& values) {
int numItems = values.size();
// Initialize the dp array
std::vector<std::vector<int>> dp(numItems + 1, std::vector<int>(maxWeight + 1, 0));
// Build the dp array
for (int itemIndex = 1; itemIndex <= numItems; ++itemIndex) {
for (int currentWeight = 0; currentWeight <= maxWeight; ++currentWeight) {
// If weight of the current item is less than or equal to the current capacity
if (weights[itemIndex - 1] <= currentWeight) {
// Choose the maximum of including the item or not including it
dp[itemIndex][currentWeight] = std::max(
dp[itemIndex - 1][currentWeight],
dp[itemIndex - 1][currentWeight - weights[itemIndex - 1]] + values[itemIndex - 1]
);
} else {
// If the item cannot be included, carry forward the value without it
dp[itemIndex][currentWeight] = dp[itemIndex - 1][currentWeight];
}
}
}
// The maximum value that can be put in the knapsack of capacity maxWeight
return dp[numItems][maxWeight];
}
};
class Solution:
def knapSack(self, maxWeight, weights, values):
numItems = len(values)
# Initialize the dp array
dp = [[0] * (maxWeight + 1) for _ in range(numItems + 1)]
# Build the dp array
for itemIndex in range(1, numItems + 1):
for currentWeight in range(maxWeight + 1):
# If weight of the current item is less than or equal to the current capacity
if weights[itemIndex - 1] <= currentWeight:
# Choose the maximum of including the item or not including it
dp[itemIndex][currentWeight] = max(
dp[itemIndex - 1][currentWeight],
dp[itemIndex - 1][currentWeight - weights[itemIndex - 1]] + values[itemIndex - 1]
)
else:
# If the item cannot be included, carry forward the value without it
dp[itemIndex][currentWeight] = dp[itemIndex - 1][currentWeight]
# The maximum value that can be put in the knapsack of capacity maxWeight
return dp[numItems][maxWeight]
Time Complexity
- Building the DP Array:
The function iterates through each item and each possible weight capacity. Therefore, the time complexity for filling the DP array is \( O(\text{numItems} \times \text{maxWeight}) \), where \( \text{numItems} \) is the number of items and \( \text{maxWeight} \) is the maximum weight capacity of the knapsack.
- Overall Time Complexity:
The overall time complexity is \( O(\text{numItems} \times \text{maxWeight}) \), combining the time required to build the DP array.
Space Complexity
- DP Array:
The DP array requires \( O(\text{numItems} \times \text{maxWeight}) \) space to store the maximum value for each item and weight capacity combination.
- Overall Space Complexity:
The overall space complexity is \( O(\text{numItems} \times \text{maxWeight}) \), due to the space needed for the DP array.