You are given two lists of integers weights and values which have the same length and an integer capacity. weights[i] and values[i] represent the weight and value of the ith item.
Given that you can take at most capacity weights, and that you can take any number of copies for each item, return the maximum amount of value you can get.
Constraints
n ≤ 250 where n is the length of weights and values
capacity ≤ 250
Example 1
Input
weights = [1, 2, 3]
values = [1, 5, 3]
capacity = 5
Output
11
Unbounded Knapsack Problem solved by Dynamic Programming Algorithm
This is a typical unbounded knapsack problem where you can pack as many as items per item-type as long as there is enough capacity. The DP (Dynamic Programming) equation is:
1 2 3 4 5 6 7 8 | class Solution: def uboundedKnapsack(self, weights, values, capacity): dp = [0] * (capacity + 1) for i in range(1, capacity + 1): for j in range(len(weights)): if i >= weights[j]: dp[i] = max(dp[i], dp[i - weights[j]] + values[j]) return max(dp) |
class Solution: def uboundedKnapsack(self, weights, values, capacity): dp = [0] * (capacity + 1) for i in range(1, capacity + 1): for j in range(len(weights)): if i >= weights[j]: dp[i] = max(dp[i], dp[i - weights[j]] + values[j]) return max(dp)
The above Python implementation of Dynamic Programming to solve the Unbounded Knapsack problem has time complexity O(CW) and space complexity O(C).
C++ implementation of the same DP algorithm:
1 2 3 4 5 6 7 8 9 10 11 | int uboundedKnapsack(vector<int>& weights, vector<int>& values, int capacity) { vector<int> dp(capacity + 1, 0); for (int i = 1; i <= capacity; ++ i) { for (int j = 0; j < weights.size(); ++ j) { if (i >= weights[j]) { dp[i] = max(dp[i], dp[i - weights[j]] + values[j]); } } } return *max_element(begin(dp), end(dp)); } |
int uboundedKnapsack(vector<int>& weights, vector<int>& values, int capacity) { vector<int> dp(capacity + 1, 0); for (int i = 1; i <= capacity; ++ i) { for (int j = 0; j < weights.size(); ++ j) { if (i >= weights[j]) { dp[i] = max(dp[i], dp[i - weights[j]] + values[j]); } } } return *max_element(begin(dp), end(dp)); }
See also: Dynamic Programming Algorithm to Solve 0-1 Knapsack Problem
Knapsack Problems
- Teaching Kids Programming - 0/1 Knapsack: Length of the Longest Subsequence That Sums to Target (Recursion+Memoization=Top Down Dynamic Programming)
- Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm
- Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem
- Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm
- Teaching Kids Programming - Multiple Strange Coin Flips/Toss Bottom Up Dynamic Programming Algorithm (Knapsack Variant)
- Teaching Kids Programming - Multiple Strange Coin Flips/Toss Top Down Dynamic Programming Algorithm (Knapsack Variant)
- Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Bottom Up Dynamic Programming Algorithm
- Teaching Kids Programming - Max Profit of Rod Cutting (Unbounded Knapsack) via Top Down Dynamic Programming Algorithm
- Teaching Kids Programming - Brick Layout (Unlimited Knapsack) via Bottom Up Dynamic Programming Algorithm
- Teaching Kids Programming - Brick Layout (Unlimited Knapsack) via Top Down Dynamic Programming Algorithm
- Count Multiset Sum (Knapsacks) by Dynamic Programming Algorithm
- Count Multiset Sum (Knapsacks) by Recursive BackTracking Algorithm
- Dynamic Programming Algorithm to Solve the Poly Knapsack (Ubounded) Problem
- Dynamic Programming Algorithm to Solve 0-1 Knapsack Problem
- Classic Unlimited Knapsack Problem Variant: Coin Change via Dynamic Programming and Depth First Search Algorithm
- Classic Knapsack Problem Variant: Coin Change via Dynamic Programming and Breadth First Search Algorithm
- Complete Knapsack Problem
- 0/1 Knapsack Problem
- Teaching Kids Programming - Combination Sum Up to Target (Unique Numbers) by Dynamic Programming Algorithms
- Algorithms Series: 0/1 BackPack - Dynamic Programming and BackTracking
- Using BackTracking Algorithm to Find the Combination Integer Sum
- Facing Heads Probabilties of Tossing Strange Coins using Dynamic Programming Algorithm
- Partition Equal Subset Sum Algorithms using DFS, Top-Down and Bottom-up DP
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Teaching Kids Programming - Find the Single Number in Array
Next Post: Teaching Kids Programming - Algorithms to Determine a Happy Number