Ctrl K

Roster Without Grudges

Medium
A coach is picking a squad for a tournament. Player i has a rating ratings[i] and an age ages[i]. The strength of a squad is the sum of the ratings of everyone in it. The squad must be free of grudges: a grudge arises when one player is strictly younger than another yet has a strictly higher rating. Players of the same age never hold grudges. Return the greatest strength a squad can have. A squad of a single player is allowed.
Example 1
ratings=[6, 4, 8, 2], ages=[30, 25, 22, 40] → 10

The 22-year-old rated 8 outrates every older player, so a roster with him holds just him: 8. Without him, take the players aged 25 and 30: 4 + 6 = 10. The 40-year-old rated 2 cannot join them, since younger players outrate him. The answer is 10.

Example 2
ratings=[7, 3, 5], ages=[20, 20, 20] → 15

Everyone is the same age, so there are no conflicts: take all of them, 7 + 3 + 5 = 15.

Example 3
ratings=[9], ages=[50] → 9
Constraints
  • 1 ≤ len(ratings) = len(ages) ≤ 1000
  • 1 ≤ ratings[i] ≤ 10⁶
  • 1 ≤ ages[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsChain of Multiples MediumFewest Meetings to Cancel MediumLongest Chain of Segments MediumWords Hidden in a Text MediumTrees Built From Products Medium
loading Python…
Input
ratings=[6, 4, 8, 2], ages=[30, 25, 22, 40]
Expected
10

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