Unit Weight Knapsack
Asked at Google
Reported by the problem's creator, not verified
You are given several item types. Type i has value values[i] and counts[i] available copies. Every copy weighs 1, and a bag holds at most capacity copies in total.
Implement max_value(values, counts, capacity) that returns the largest possible sum of values of copies placed in the bag.
Examples
Example 1
Input: values =
[5,3,8], counts =[2,1,1], capacity =3Output:
18Explanation: The best three copies are one 8 and two 5s, for 8 + 5 + 5 = 18.
Example 2
Input: values =
[-2,4], counts =[5,1], capacity =10Output:
4Explanation: Type 0 has five copies worth -2 each, and type 1 has one copy worth 4. Taking any -2 copy would lower the total, so the bag skips all of them. Taking the single 4 copy gives the largest total, 4.