MediumNo. 1090
Largest Values From Labels — LeetCode 1090 Python Solution
- Pattern
- Greedy
- Topics
- Greedy, Array, Hash Table, Counting, Sorting
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given n item's value and label as two integer arrays values and labels. You are also given two integers numWanted and useLimit.
Python solution
class Solution:
def largestValsFromLabels(
self, values: List[int], labels: List[int], numWanted: int, useLimit: int
) -> int:
ans = num = 0
cnt = Counter()
for v, l in sorted(zip(values, labels), reverse=True):
if cnt[l] < useLimit:
cnt[l] += 1
num += 1
ans += v
if num == numWanted:
break
return ansComplexity
- Time
- O(n)
- Space · auxiliary
- O(n)
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 1090. Largest Values From Labels is filed here because LeetCode tags it Greedy, which is the vocabulary this hub collects.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0621 | Task Scheduler | Medium |
| 1054 | Distant Barcodes | Medium |
| 1481 | Least Number of Unique Integers after K Removals | Medium |
| 0169 | Majority Element | Easy |
| 0229 | Majority Element II | Medium |
| 0347 | Top K Frequent Elements | Medium |
Frequently asked questions
- How hard is LeetCode 1090. Largest Values From Labels?
LeetCode 1090. Largest Values From Labels is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1090. Largest Values From Labels?
The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1090. Largest Values From Labels?
The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 1090. Largest Values From Labels cover?
LeetCode 1090. Largest Values From Labels is tagged Greedy, Array, Hash Table, Counting and Sorting on LeetCode.