1766. Two Squads Draft
A coach is drafting two squads from a pool of n students. Student i is worth a[i] points if placed on squad A and b[i] points if placed on squad B. Squad A must have exactly p students and squad B exactly q students, no student can be on both, and the remaining n - p - q students are not drafted at all.
Return the maximum possible total of points over both squads. Either p or q may be zero, and p + q never exceeds n. The total can exceed 32 bits, so return a long. Hint: sort the students by a[i] - b[i] in descending order; some optimal draft puts every squad A student before every squad B student in that order. Aim for O(n log n) time and O(n) space.
Example 1
- Input:
- a = [5,1,3]b = [1,4,2]p = 1q = 1
- Output:
- 9
- Explanation:
Put student 0 on squad A for 5 points and student 1 on squad B for 4 points, for a total of 9.
Example 2
- Input:
- a = [10,1]b = [1,10]p = 1q = 1
- Output:
- 20
- Explanation:
Everyone is drafted: student 0 goes to squad A (10) and student 1 to squad B (10).
Example 3
- Input:
- a = [4,4,4]b = [4,4,4]p = 2q = 0
- Output:
- 8
- Explanation:
Only squad A is drafted (q = 0), so the two chosen students give 4 + 4 = 8.
Constraints
1 ≤ n ≤ 105, where n = a.length = b.length
0 ≤ p, q and p + q ≤ n
1 ≤ a[i], b[i] ≤ 109
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 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)