Maximum Elegance of a K-Length Subsequence Problem
Description
LeetCode Problem 2813.
You are given a 0-indexed 2D integer array items of length n and an integer k.
items[i] = [profit_i, category_i], where profit_i and category_i denote the profit and category of the i^th item respectively.
Let's define the elegance of a subsequence of items as total_profit + distinct_categories^2, where total_profit is the sum of all profits in the subsequence, and distinct_categories is the number of distinct categories from all the categories in the selected subsequence.
Your task is to find the maximum elegance from all subsequences of size k in items.
Return an integer denoting the maximum elegance of a subsequence of items with size exactly k.
Note: A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order.
Example 1:
Input: items = [[3,2],[5,1],[10,1]], k = 2
Output: 17
Explanation: In this example, we have to select a subsequence of size 2.
We can select items[0] = [3,2] and items[2] = [10,1].
The total profit in this subsequence is 3 + 10 = 13, and the subsequence contains 2 distinct categories [2,1].
Hence, the elegance is 13 + 2^2 = 17, and we can show that it is the maximum achievable elegance.
Example 2:
Input: items = [[3,1],[3,1],[2,2],[5,3]], k = 3
Output: 19
Explanation: In this example, we have to select a subsequence of size 3.
We can select items[0] = [3,1], items[2] = [2,2], and items[3] = [5,3].
The total profit in this subsequence is 3 + 2 + 5 = 10, and the subsequence contains 3 distinct categories [1,2,3].
Hence, the elegance is 10 + 3^2 = 19, and we can show that it is the maximum achievable elegance.
Example 3:
Input: items = [[1,1],[2,1],[3,1]], k = 3
Output: 7
Explanation: In this example, we have to select a subsequence of size 3.
We should select all the items.
The total profit will be 1 + 2 + 3 = 6, and the subsequence contains 1 distinct category [1].
Hence, the maximum elegance is 6 + 1^2 = 7.
Constraints:
1 <= items.length == n <= 10^5items[i].length == 2items[i][0] == profit_iitems[i][1] == category_i1 <= profit_i <= 10^91 <= category_i <= n1 <= k <= n
Solution
Approach: Greedy
Sort the items by profit, largest first, and take the first k. Record their total profit, the set of categories seen, and, in a stack, the profits of items whose category was already present. Items were added in decreasing profit, so the top of the stack is the cheapest duplicate.
Then scan the remaining items. An item of an already used category cannot add a new category, so skip it. If there is no duplicate left to give up, skip as well. Otherwise swap out the cheapest duplicate (the top of the stack) for this item: the profit changes by p - removed and the number of categories grows by one. Update the best elegance, profit + categories^2, after each swap, and return the best value seen.
Complexity: Time O(n log n), space O(n).
class Solution:
def findMaximumElegance(self, items: List[List[int]], k: int) -> int:
items.sort(key=lambda x: -x[0])
tot = 0
vis = set()
dup = []
for p, c in items[:k]:
tot += p
if c not in vis:
vis.add(c)
else:
dup.append(p)
ans = tot + len(vis) ** 2
for p, c in items[k:]:
if c in vis or not dup:
continue
vis.add(c)
tot += p - dup.pop()
ans = max(ans, tot + len(vis) ** 2)
return ans