BackhardTwo Pointers

Constrained Collection Optimization Solution

Problem Statement

Given a set of items, each with a weight and a value, determine the optimal subset to select such that the total weight does not exceed a specified limit and the total value is maximized. Note: There may be multiple optimal subsets.

Example 1
Input
items = [(3, 60), (2, 100), (4, 120)], weight_limit = 5
Output
undefined

Explanation: The optimal subset is [(4, 120)] since its total value is maximized without exceeding the weight limit.

Example 2
Input
items = [(1, 10), (3, 40), (2, 50)], weight_limit = 4
Output
undefined

Explanation: One of the optimal subsets is [(3, 40), (1, 10)] since its total value is maximized without exceeding the weight limit. Another optimal subset is [(2, 50)].

Constraints

  • 1 <= number of items <= 50
  • 1 <= weight of each item <= 10
  • 1 <= value of each item <= 1000
  • 1 <= weight limit <= 50
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free