Quick Overview

Given item values, item weights and a weight capacity, find the largest total value of a subset of items that fits, where each item is taken whole or not at all and at most once. It tests the take-or-skip recurrence, a compact table over capacities, and the update order that keeps an item from being counted twice.

Maximize the value of items packed under a weight limit, each item taken at most once

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given the values and weights of `n` items and a knapsack that can carry a total weight of at most `capacity`. Item `i` has value `values[i]` and weight `weights[i]`. Determine the maximum total value that can be placed in the knapsack. Each item is either included or excluded (the 0/1 property). You cannot include a fractional part of an item, and each item can be selected at most once. Return the maximum value that fits within the knapsack capacity. ### Function Signature ```python def max_knapsack_value(values: list[int], weights: list[int], capacity: int) -> int: ``` ### Rules - `values[i]` and `weights[i]` describe the same item, and the two lists have the same length. - The total weight of the chosen items must be at most `capacity`. It does not have to equal `capacity`. - Choosing no items is allowed, so the answer is `0` when no item fits. ### Constraints - `1 <= n <= 1000`, where `n = len(values) = len(weights)` - `1 <= capacity <= 1000` - `1 <= values[i] <= 1000` - `1 <= weights[i] <= 1000` ### Examples ```text Input: values = [60, 100, 120], weights = [10, 20, 30], capacity = 50 Output: 220 ``` Take the items with weights 20 and 30: total weight 50, total value 100 + 120 = 220. All three items together weigh 60, which is over the limit, and the other two pairs give 160 (weights 10 and 20) and 180 (weights 10 and 30). ```text Input: values = [10, 40, 30, 50], weights = [5, 4, 6, 3], capacity = 10 Output: 90 ``` Take the items with weights 4 and 3: total weight 7, total value 40 + 50 = 90. Any three items weigh at least 3 + 4 + 5 = 12, and the best other pair (weights 6 and 3) is worth 80. ```text Input: values = [7, 3, 4], weights = [12, 2, 3], capacity = 4 Output: 4 ``` The first item is heavier than the knapsack, and the other two together weigh 5. The best single item that fits is the one with weight 3 and value 4.

Overview: Given item values, item weights and a weight capacity, find the largest total value of a subset of items that fits, where each item is taken whole or not at all and at most once. It tests the take-or-skip recurrence, a compact table over capacities, and the update order that keeps an item from being counted twice.

You are given `n` items and a knapsack that can carry a total weight of at most `capacity`. Item `i` has value `values[i]` and weight `weights[i]`. Find the largest total value you can place in the knapsack. Each item is either taken whole or left out. You cannot take part of an item, and each item can be taken at most once. ### Rules - `values[i]` and `weights[i]` describe the same item, and the two lists have the same length. - The total weight of the chosen items must be at most `capacity`. It does not have to equal `capacity`. - Choosing no items is allowed, so the answer is `0` when no item fits. An item may weigh more than `capacity`, in which case it can never be chosen. Return the maximum total value as an integer. The answer is at most 1,000 * 1,000 = 1,000,000, so it fits in a 32-bit signed integer in every language. ### Example 1 ``` Input: values = [60, 100, 120], weights = [10, 20, 30], capacity = 50 Output: 220 ``` Take the items with weights 20 and 30: total weight 50, total value 100 + 120 = 220. All three items together weigh 60, which is over the limit, and the other two pairs give 160 (weights 10 and 20) and 180 (weights 10 and 30). ### Example 2 ``` Input: values = [10, 40, 30, 50], weights = [5, 4, 6, 3], capacity = 10 Output: 90 ``` Take the items with weights 4 and 3: total weight 7, total value 40 + 50 = 90. Any three items weigh at least 3 + 4 + 5 = 12, and the best other pair (weights 6 and 3) is worth 80. ### Constraints - `1 <= n <= 1000`, where `n = len(values) = len(weights)` - `1 <= capacity <= 1000` - `1 <= values[i] <= 1000` - `1 <= weights[i] <= 1000`

Constraints

  • 1 <= n <= 1000, where n = len(values) = len(weights)
  • 1 <= capacity <= 1000
  • 1 <= values[i] <= 1000
  • 1 <= weights[i] <= 1000

Examples

Input: ([60, 100, 120], [10, 20, 30], 50)

Expected Output: 220

Explanation: First worked example: weights 20 and 30 fill the limit for 220, while a value-per-weight greedy stops at 160.

Input: ([10, 40, 30, 50], [5, 4, 6, 3], 10)

Expected Output: 90

Explanation: Second worked example: weights 4 and 3 give 90 at total weight 7; requiring an exact fill of 10 would give only 70.

Hints

  1. Try always picking the item with the best value per unit of weight on the first example. That rule takes the weight-10 and weight-20 items and ends at 160, below the correct 220.
  2. For each item there are only two choices: take it or leave it. What do you need to know about the items you have already decided on to make the next choice well?
  3. Remember that choosing nothing is allowed. If no item fits, the answer is 0, and the best set does not have to fill the knapsack completely.

Maximum Value Under a Knapsack Weight Limit

mediumGeneralSoftware Engineer

You are given n items and a knapsack that can carry a total weight of at most capacity. Item i has value values[i] and weight weights[i]. Find the largest total value you can place in the knapsack.

Each item is either taken whole or left out. You cannot take part of an item, and each item can be taken at most once.

Rules

  • values[i] and weights[i] describe the same item, and the two lists have the same length.
  • The total weight of the chosen items must be at most capacity. It does not have to equal capacity.
  • Choosing no items is allowed, so the answer is 0 when no item fits. An item may weigh more than capacity, in which case it can never be chosen.

Return the maximum total value as an integer. The answer is at most 1,000 * 1,000 = 1,000,000, so it fits in a 32-bit signed integer in every language.

Example 1

Input: values = [60, 100, 120], weights = [10, 20, 30], capacity = 50
Output: 220

Take the items with weights 20 and 30: total weight 50, total value 100 + 120 = 220. All three items together weigh 60, which is over the limit, and the other two pairs give 160 (weights 10 and 20) and 180 (weights 10 and 30).

Example 2

Input: values = [10, 40, 30, 50], weights = [5, 4, 6, 3], capacity = 10
Output: 90

Take the items with weights 4 and 3: total weight 7, total value 40 + 50 = 90. Any three items weigh at least 3 + 4 + 5 = 12, and the best other pair (weights 6 and 3) is worth 80.

Constraints

  • 1 <= n <= 1000, where n = len(values) = len(weights)
  • 1 <= capacity <= 1000
  • 1 <= values[i] <= 1000
  • 1 <= weights[i] <= 1000

Examples

Example 1
Input
([60, 100, 120], [10, 20, 30], 50)
Output
220
Notes
First worked example: weights 20 and 30 fill the limit for 220, while a value-per-weight greedy stops at 160.
Example 2
Input
([10, 40, 30, 50], [5, 4, 6, 3], 10)
Output
90
Notes
Second worked example: weights 4 and 3 give 90 at total weight 7; requiring an exact fill of 10 would give only 70.

Constraints

  • •1 <= n <= 1000, where n = len(values) = len(weights)
  • •1 <= capacity <= 1000
  • •1 <= values[i] <= 1000
  • •1 <= weights[i] <= 1000

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_knapsack_value(values: list[int], weights: list[int], capacity: int) -> int:
Sample call
max_knapsack_value([60, 100, 120], [10, 20, 30], 50)
Language
Case 1
Input
([60, 100, 120], [10, 20, 30], 50)
Expected
220
Case 2
Input
([10, 40, 30, 50], [5, 4, 6, 3], 10)
Expected
90

Browse More Questions

Show the approach

Approach

Keep an array best of length capacity + 1, where best[c] is the largest total value of a subset of the items processed so far whose total weight is at most c. Before any item is processed every entry is 0, which stands for the empty choice.

To add an item with value v and weight w, every limit c with c >= w takes the larger of best[c] (leave the item out) and best[c - w] + v (take it once). Both options must read the array as it was before this item, or the item could be counted twice. The Python reference copies the two slices before writing, and the JavaScript, Java and C++ references walk c from capacity down to w, so best[c - w] still holds its old value when it is read. Limits below w cannot hold the item and keep their old values, and an item heavier than capacity is skipped entirely.

Correctness follows by induction on the number of processed items. An optimal subset for limit c either leaves out the newest item, in which case its value is the previous best[c], or contains it exactly once, in which case the rest weighs at most c - w and is worth at most the previous best[c - w]. Both options are considered, so best[c] stays exact. After all items, best[capacity] covers every subset whose total weight is at most capacity, which is the answer.

Edge cases: when no item fits, no entry ever changes and the answer is 0. Because each entry means at most c rather than exactly c, a best subset lighter than capacity is still counted. The largest possible answer is 1,000 * 1,000 = 1,000,000, so 32-bit integers are enough in every language.

Time complexity:
O(n * capacity)
Space complexity:
O(capacity)