Ctrl K

Best Bonuses From Lower Ranks

Medium
A team has n employees. Employee i has a rank ranks[i] and a bonus bonuses[i]. For each employee i, look at everyone whose rank is **strictly lower** than ranks[i] and pick at most k of their bonuses so that the total is as large as possible. If nobody has a lower rank, the total is 0. Return a list of length n whose i-th element is that maximum total for employee i.
Example 1
ranks=[3, 1, 4, 2], bonuses=[7, 9, 5, 6], k=2 → [15, 0, 16, 9]

Employee 0 (rank 3) outranks employees 1 and 3 with bonuses 9 and 6, giving 15. Employee 1 outranks nobody, so 0. Employee 2 (rank 4) outranks everyone else; the two best bonuses are 9 and 7, giving 16. Employee 3 (rank 2) outranks only employee 1, giving 9.

Example 2
ranks=[5, 5, 2], bonuses=[4, 8, 3], k=3 → [3, 3, 0]

An equal rank does not count as lower: employees 0 and 1 can only take employee 2's bonus, which is 3.

Example 3
ranks=[1, 2, 3, 4], bonuses=[10, 1, 1, 1], k=1 → [0, 10, 10, 10]
Constraints
  • 1 ≤ n ≤ 10⁵
  • len(ranks) == len(bonuses) == n
  • 1 ≤ ranks[i], bonuses[i] ≤ 10⁶
  • 1 ≤ k ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsK-th Highest Score MediumStudios for Recording Sessions MediumMost Common Values MediumK-th Smallest in a Sorted Grid MediumJobs With a Cooldown Medium
loading Python…
Input
ranks=[3, 1, 4, 2], bonuses=[7, 9, 5, 6], k=2
Expected
[15, 0, 16, 9]

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