Ctrl K

Best Picks in Order

Easy
Judges gave the performers the scores scores, listed in the order they performed. Exactly count performers advance to the final, and their combined score must be as large as possible. Return the finalists' scores in the order they performed. If more performers share a score than there are spots left for it, the ones who performed later (higher indices) advance, so the answer is unique.
Example 1
scores=[4, -2, 9, 1, 7], count=3 → [4, 9, 7]

The three largest values are 4, 9 and 7. In their original order that is [4, 9, 7], with sum 20.

Example 2
scores=[5, 8, 5, 5], count=2 → [8, 5]

We take 8 and one of the 5s. There are three 5s, and the rule says to take the rightmost one, so the answer is [8, 5], not [5, 8].

Example 3
scores=[-3, -1, -4], count=1 → [-1]

All values are negative; the best one is -1.

Constraints
  • 1 ≤ len(scores) ≤ 10⁵
  • -10⁵ ≤ scores[i] ≤ 10⁵
  • 1 ≤ count ≤ len(scores)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAverage of the Best Five EasyClearing the Stacks Evenly EasyMost Common Values MediumJobs With a Cooldown MediumNarrowest Range Touching Every List Hard
loading Python…
Input
scores=[4, -2, 9, 1, 7], count=3
Expected
[4, 9, 7]

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.