948. Conflict-Free Team

Medium3.5 points estimatedArrayDynamic ProgrammingSorting3 hints

A club is picking a relay squad from athletes described by two arrays: scores[i] is the i-th athlete's rating and ages[i] is their age. The squad's strength is the sum of its members' ratings. A squad is allowed only if it has no clash: a clash is a pair where the younger athlete has a strictly higher rating than the older one. Athletes of equal age never clash, and equal ratings never clash.

Return the largest total rating of any clash-free squad. A squad may consist of a single athlete, and it may include any subset of the athletes.

Example 1

Input:
scores = [6,3,9,4], ages = [20,25,22,24]
Output:
15
Explanation:

By age the ratings read 6, 9, 4, 3; the best non-decreasing choice is 6 + 9 = 15, since adding 4 or 3 would make a younger athlete out-rate an older one.

Example 2

Input:
scores = [5,7,7], ages = [30,30,30]
Output:
19
Explanation:

Everyone has the same age, so nobody clashes and the whole squad scores 5 + 7 + 7 = 19.

Example 3

Input:
scores = [8,6,2], ages = [18,21,40]
Output:
8
Explanation:

Ratings fall as age rises, so any two athletes clash; the best squad is the single athlete rated 8.

Constraints

  • 1 ≤ scores.length == ages.length ≤ 105
  • 1 ≤ scores[i] ≤ 104
  • 1 ≤ ages[i] ≤ 105

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 680 msC++ 170 msJava 340 msJavaScript 340 msTypeScript 340 ms

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…