Unit Weight Knapsack

Asked at GooglePhone screen

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

  1. Example 1

    Input: values = [5,3,8], counts = [2,1,1], capacity = 3

    Output: 18

    Explanation: The best three copies are one 8 and two 5s, for 8 + 5 + 5 = 18.

  2. Example 2

    Input: values = [-2,4], counts = [5,1], capacity = 10

    Output: 4

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.