1
from collections import defaultdict6
def largestValsFromLabels(7
self, values: list[int], labels: list[int], numWanted: int, useLimit: int10
# Add labels and values into the heap11
heap = [(-value, label) for value, label in zip(values, labels)]14
# Initialize the hashmap15
used = defaultdict(int)17
# Initialize the result20
# Iterate until we have used a certain number or the heap is empty21
while numWanted > 0 and heap:23
# Pop a label and its value from the heap24
value, label = heapq.heappop(heap)26
# If we can use such label27
if used[label] < useLimit:29
# Add its value to the result32
# Increment its count in the hashmap35
# Decrement the number of numbers we still want