Skip to content

Commit a556523

Browse files
authored
Create largest-values-from-labels.py
1 parent 23292f7 commit a556523

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Python/largest-values-from-labels.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def largestValsFromLabels(self, values, labels, num_wanted, use_limit):
9+
"""
10+
:type values: List[int]
11+
:type labels: List[int]
12+
:type num_wanted: int
13+
:type use_limit: int
14+
:rtype: int
15+
"""
16+
counts = collections.defaultdict(int)
17+
val_labs = zip(values,labels)
18+
val_labs.sort(reverse=True)
19+
result = 0
20+
for val, lab in val_labs:
21+
if counts[lab] >= use_limit:
22+
continue
23+
result += val
24+
counts[lab] += 1
25+
num_wanted -= 1
26+
if num_wanted == 0:
27+
break
28+
return result

0 commit comments

Comments
 (0)