Leetcode 1090: Largest Values From Labels

grid47
grid47
Exploring patterns and algorithms
Jul 21, 2024 7 min read

You are given two arrays, values and labels, representing the value and label of items, respectively. Your goal is to choose a subset of items with the maximum sum of values such that the number of items selected is at most numWanted and no label appears more than useLimit times in the subset. Return the maximum possible sum of the values from the selected subset of items.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays: `values` and `labels` of length `n`, where each element in `values` represents the value of the corresponding item, and each element in `labels` represents the label of the corresponding item. Additionally, you are given two integers: `numWanted` (the maximum number of items that can be selected) and `useLimit` (the maximum number of items that can be selected with the same label).
Example: Input: values = [10, 8, 5, 4, 3], labels = [1, 1, 2, 2, 3], numWanted = 3, useLimit = 1
Constraints:
• n == values.length == labels.length
• 1 <= n <= 2 * 10^4
• 0 <= values[i], labels[i] <= 2 * 10^4
• 1 <= numWanted, useLimit <= n
Output: Return the maximum sum of the values from the selected subset of items that satisfies the constraints on selection and label usage.
Example: Output: 18
Constraints:
• The maximum sum should be obtained by selecting the best possible combination of items while respecting the label constraints.
Goal: The goal is to select the subset of items that yields the maximum sum of values while ensuring that no more than `numWanted` items are chosen and that no label appears more than `useLimit` times.
Steps:
• 1. Sort the items by their values in descending order to prioritize the highest value items.
• 2. Track the frequency of each label as items are selected.
• 3. Select items one by one while ensuring that the total number of selected items does not exceed `numWanted`, and no label appears more than `useLimit` times.
Goal: The solution must efficiently handle large inputs while adhering to the problem's constraints.
Steps:
• 1 <= n <= 2 * 10^4
• The solution should be optimized to handle up to 20,000 items.
• The input arrays `values` and `labels` contain integer values between 0 and 20,000.
Assumptions:
• The input arrays `values` and `labels` are non-empty and contain valid values within the specified range.
Input: Input: values = [10, 8, 5, 4, 3], labels = [1, 1, 2, 2, 3], numWanted = 3, useLimit = 1
Explanation: The optimal subset of items with the highest values while respecting the label constraints is the first, third, and fifth items, yielding a total value of 10 + 5 + 3 = 18.

Input: Input: values = [9, 8, 7, 6, 5], labels = [0, 0, 1, 1, 0], numWanted = 3, useLimit = 2
Explanation: In this example, the best subset is the first, second, and third items, which gives the maximum sum of values: 9 + 8 + 7 = 24.

Input: Input: values = [5, 4, 3, 2, 1], labels = [1, 1, 2, 2, 3], numWanted = 3, useLimit = 1
Explanation: The subset chosen is the first, third, and fifth items with the sum of values: 5 + 3 + 1 = 9.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus