KatInterview

Maximize Total Score by Pairing Players Optimally

Greedy & IntervalsAuto-graded · 19 test cases
MediumAuto-gradedGreedy & Intervalsgreedysortingarrays

Last 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^5
  • len(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.