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

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.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

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

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).

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.

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.