Only the Best Count
A quiz league keeps only your best results. scores lists every score you have made so far, and only the k highest of them count. Equal scores are separate results, so the same value can count more than once.
Return the total of the scores that count. If you have fewer than k scores, all of them count.
Example 1
scores [70,95,80,60]
k 2
returns 175
The two highest scores are 95 and 80, and 95 + 80 = 175.
Example 2
scores [50,50,50]
k 2
returns 100
Two of the three equal scores count: 50 + 50 = 100.
Example 3
scores [40,90]
k 5
returns 130
There are only two scores, so both count: 40 + 90 = 130.
Constraints
0 <= len(scores) <= 300 <= scores[i] <= 1001 <= k <= 30
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Greedy problems
- Shortest Walk to SeatsBeginner
- Stepping StonesBeginner
- Third One FreeBeginner
- Two Numbers From Four DigitsBeginner
- Array PartitionEasy
- Assign CookiesEasy
- Best Haul With Partial SacksEasy
- Best Time to Trade Stock Many TimesEasy