Maximize Total Score by Pairing Players Optimally
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests whether a candidate can apply a greedy sort-and-pair strategy to maximize the sum of pair minimums across a list of player ratings.
Problem Description
You are organizing a two-player game tournament. Each participant has a numeric skill rating. Before the games begin, you must divide all participants into duos. Every participant belongs to exactly one duo.
When a duo plays, only the weaker player's rating counts toward the final score — the stronger player's rating is ignored. In other words, each duo contributes exactly the smaller of its two members' ratings to the overall score.
Your goal is to assign participants to duos in a way that makes the total score as large as possible.
Given a list of integers ratings representing each participant's skill rating, return the highest possible total score.
Constraints
2 <= len(ratings) <= 10^5len(ratings)is always even.1 <= ratings[i] <= 10^4
Examples
Example 1
Input: max_total_score([1, 4, 2, 3])
Output: 4
Example 2
Input: max_total_score([6, 2, 6, 5, 1, 2])
Output: 9
Hints
Hint 1
What happens to the stronger player's rating inside each duo? Is there a pairing where that loss is minimized?
Hint 2
Try sorting the ratings. Which pairings waste the least potential?
Hint 3
After sorting, look at what values actually end up contributing to the sum when you pair adjacent elements.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key observation is that inside every duo, exactly one rating is discarded — the larger one. To maximize the total, we want to minimize the total amount discarded.
Greedy insight: sort all ratings in ascending order, then pair up consecutive neighbors: (a[0], a[1]), (a[2], a[3]), and so on. Each duo's contribution is its left (smaller) element, so the answer is the sum of every even-indexed element after sorting.
Why this is correct — exchange argument:
Take the globally smallest rating a[0]. Whatever it is paired with, that partner's rating is completely wasted (because a[0] is always the minimum). We should therefore sacrifice the cheapest possible partner, which is a[1].
Suppose an optimal solution instead pairs a[0] with some larger value x, and pairs a[1] with some value y. If we swap to make the duos (a[0], a[1]) and (x, y):
- Old contribution from these two duos:
a[0] + min(a[1], y) - New contribution:
a[0] + min(x, y)
Since both x ≥ a[1] and y ≥ a[1], we have min(x, y) ≥ a[1] ≥ min(a[1], y), so the swap never makes things worse. Remove that duo and apply the same argument inductively to the remaining sorted elements.
Why this is correct — upper bound argument:
Sort descending. The largest possible value for the k-th biggest duo minimum is b[2k-1] (because at least 2k-1 elements must be at least as large as it). Therefore the maximum achievable total is b[1] + b[3] + ..., which is exactly the sum of odd positions in descending order — equivalently, even positions in ascending order. Adjacent pairing achieves this bound exactly.
Edge cases:
- Two participants: only one duo is possible, so the answer is simply
min(a, b). After sorting, that isa[0], which is correct. - All ratings equal: every duo contributes the same value
c, and the answer isc * n/2. Adjacent pairing handles this naturally. - Wide rating ranges: the maximum possible sum is
5 * 10^4 * 10^4 = 5 * 10^8, well within Python's integer range.
Complexity:
- Time:
O(n log n)for the sort; the summation isO(n). - Space:
O(1)extra (Python's Timsort uses up toO(n)auxiliary space internally; the sliceratings[::2]allocatesO(n/2)but can be replaced with an index loop if memory is a concern).
Note that this solution sorts the input list in place, mutating the caller's data. Use sorted(ratings) instead if the original order must be preserved.
from typing import List
def max_total_score(ratings: List[int]) -> int:
# Sort so that adjacent elements are as close together as possible.
# Pairing (a[0], a[1]), (a[2], a[3]), ... means each duo's minimum
# is the element at an even index, so we sum every other element.
ratings.sort()
return sum(ratings[::2])