Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Largest Values From Labels in Python
The Largest Values From Labels problem involves selecting items from a collection to maximize the sum while respecting constraints on the total number of items and usage limits per label.
Problem Statement
Given a set of items where the i-th item has values[i] and labels[i], we need to find a subset S such that:
- |S| ? num_wanted
- For every label L, the number of items in S with label L is ? use_limit
The goal is to find the largest possible sum of the subset S.
Algorithm Approach
The solution uses a greedy approach with the following steps:
- Pair each value with its corresponding label
- Sort items by value in descending order (highest first)
- Greedily select items while respecting constraints
Example
Let's trace through the example where values = [5,4,3,2,1], labels = [1,1,2,2,3], num_wanted = 3, use_limit = 1:
class Solution:
def largestValsFromLabels(self, values, labels, num_wanted, use_limit):
# Step 1: Pair values with labels
items = []
for i in range(len(values)):
items.append([values[i], labels[i]])
# Step 2: Sort by value in descending order
items = sorted(items, key=lambda x: x[0], reverse=True)
# Step 3: Greedily select items
total_sum = 0
label_usage = {}
selected_count = 0
for value, label in items:
if selected_count >= num_wanted:
break
# Check if we can use this label
current_usage = label_usage.get(label, 0)
if current_usage < use_limit:
total_sum += value
label_usage[label] = current_usage + 1
selected_count += 1
return total_sum
# Test the solution
solution = Solution()
result = solution.largestValsFromLabels([5,4,3,2,1], [1,1,2,2,3], 3, 1)
print(f"Maximum sum: {result}")
# Let's trace the selection process
values = [5,4,3,2,1]
labels = [1,1,2,2,3]
items = list(zip(values, labels))
sorted_items = sorted(items, key=lambda x: x[0], reverse=True)
print(f"Sorted items (value, label): {sorted_items}")
Maximum sum: 9 Sorted items (value, label): [(5, 1), (4, 1), (3, 2), (2, 2), (1, 3)]
How It Works
The algorithm processes items in this order:
- (5, 1): Select (label 1 usage: 1/1) ? sum = 5
- (4, 1): Skip (label 1 already at limit)
- (3, 2): Select (label 2 usage: 1/1) ? sum = 8
- (2, 2): Skip (label 2 already at limit)
- (1, 3): Select (label 3 usage: 1/1) ? sum = 9
Alternative Implementation
Here's a more concise version using Python's built-in functions:
from collections import defaultdict
def largest_vals_from_labels(values, labels, num_wanted, use_limit):
# Create and sort items by value (descending)
items = sorted(zip(values, labels), reverse=True)
total_sum = 0
label_count = defaultdict(int)
selected = 0
for value, label in items:
if selected >= num_wanted:
break
if label_count[label] < use_limit:
total_sum += value
label_count[label] += 1
selected += 1
return total_sum
# Test with the example
result = largest_vals_from_labels([5,4,3,2,1], [1,1,2,2,3], 3, 1)
print(f"Result: {result}")
Result: 9
Time and Space Complexity
- Time Complexity: O(n log n) due to sorting
- Space Complexity: O(n) for storing the items and label usage tracking
Conclusion
The greedy approach works by selecting the highest-value items first while respecting label usage limits. This ensures we get the maximum possible sum for the given constraints.
