Largest Values From Labels - Problem

Imagine you're a treasure hunter with a collection of valuable items, each marked with different labels indicating their categories. You want to maximize your treasure value, but there are two important constraints:

  • Limited bag space: You can only carry at most numWanted items
  • Category diversity: You can't take more than useLimit items from the same category (label)

Given two arrays:

  • values - the value of each item
  • labels - the category label of each item

Your goal is to select a subset of items that maximizes the total value while respecting both constraints.

Example: If you have items with values [5,4,3,2,1] and labels [1,1,2,2,3], and you want at most 3 items with at most 1 item per label, you'd pick the items with values 5, 3, and 1 (one from each label) for a total of 9.

Input & Output

example_1.py โ€” Basic Selection
$ Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
โ€บ Output: 9
๐Ÿ’ก Note: We can select items with values 5 (label 1), 3 (label 2), and 1 (label 3). Each label appears at most once, and we selected exactly 3 items for a total of 9.
example_2.py โ€” Multiple Items Per Label
$ Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
โ€บ Output: 12
๐Ÿ’ก Note: We can select items with values 5 (label 1), 4 (label 3), and 3 (label 3). Label 3 appears twice which is within the limit of 2, giving us a total of 12.
example_3.py โ€” Limited by numWanted
$ Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 2, useLimit = 3
โ€บ Output: 17
๐Ÿ’ก Note: We can select items with values 9 and 8 (both label 0). Even though useLimit allows 3 items per label, numWanted limits us to 2 items total, giving us 9 + 8 = 17.

Constraints

  • 1 โ‰ค values.length == labels.length โ‰ค 2 * 104
  • 0 โ‰ค values[i], labels[i] โ‰ค 2 * 104
  • 1 โ‰ค numWanted, useLimit โ‰ค values.length
  • All values and labels are non-negative integers

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Treasure Hunter's Optimal Strategy๐ŸŽฏ The Challenge:โ€ข Limited bag space (numWanted = 3)โ€ข Max 1 item per category (useLimit = 1)โ€ข Maximize total treasure value๐Ÿ“Š Available Treasures (Sorted by Value):๐Ÿ’ŽValue: 5Gold ๐Ÿ†๐Ÿ’Value: 4Gold ๐Ÿ†๐ŸบValue: 3Silver ๐Ÿฅˆ๐Ÿ”ฎValue: 1Magic โœจโœ… Greedy Selection Process:๐Ÿ’ŽSelected!+5 pointsGold count: 1๐Ÿ’SkippedGold limit reached๐ŸบSelected!+3 pointsSilver count: 1๐Ÿ”ฎSelected!+1 pointMagic count: 1๐Ÿ† Final TreasureTotal: 9 pointsItems: 3/3 usedAll categories: โ‰ค1 item โœ“โšก Why Greedy Works:The key insight is that taking thehighest value item that doesn'tbreak our rules is always optimal.Time: O(n log n) - sortingSpace: O(k) - category tracking
Understanding the Visualization
1
Sort Treasures by Value
Arrange all treasures from most valuable to least valuable - this ensures we always consider the best options first
2
Greedy Selection
Go through treasures in order, picking each one if it doesn't violate our constraints (bag space and category limits)
3
Track Constraints
Keep count of items per category and total items selected to ensure we never violate the rules
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because once items are sorted by value, selecting the highest available value that doesn't violate constraints is always the optimal choice. There's no advantage to saving space for lower-value items.
Asked in
Google 42 Amazon 31 Meta 28 Microsoft 19
26.4K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen