Podium Finish: Pick the Two Best Clubs in a League Table
Heaps, Sorting & SearchAuto-graded · 24 test casesLast updated
Rank teams with a multi-level tiebreak (points, goal difference, goals scored, index) and return the best two indices, testing composite keys and efficient selection.
A league has n teams, numbered 0 to n - 1. Their results are stored in four arrays of equal length. For team i:
wins[i]is the number of matches it won,draws[i]is the number of matches it drew,scored[i]is the total goals it scored,conceded[i]is the total goals it let in.
A win is worth 3 points and a draw is worth 1 point, so team i has 3 * wins[i] + draws[i] points. Losses give no points.
Build the league table and return the indices of the team in first place and the team in second place.
Ranking rules
Compare two teams using the following criteria in order. A later criterion is consulted only if the teams are equal on every earlier one.
- The team with more points ranks higher.
- The team with the larger goal difference (
scored[i] - conceded[i], which may be negative) ranks higher. - The team with more goals scored ranks higher.
- The team with the smaller index ranks higher.
Because criterion 4 never produces a tie, the ranking is a strict order and the answer is unique.
Output
Return a list [first, second] with the index of the top-ranked team followed by the index of the second-ranked team.
Constraints
2 <= n <= 100000, wherenis the common length of the four arrays0 <= wins[i] <= 10000 <= draws[i] <= 10000 <= scored[i] <= 1000000 <= conceded[i] <= 100000- Goal difference can be negative.
Examples
Example 1
Input: top_two_clubs([1, 3, 2, 3], [2, 0, 3, 0], [5, 7, 6, 9], [5, 3, 5, 5])
Output: [3, 1]
Example 2
Input: top_two_clubs([2, 2], [1, 1], [4, 4], [2, 2])
Output: [0, 1]
Example 3
Input: top_two_clubs([0, 4, 3], [5, 0, 4], [2, 10, 3], [1, 2, 3])
Output: [2, 1]
Hints
Hint 1
Each team can be summarized by a single tuple. Python compares tuples element by element and only looks at the next element when the earlier ones are equal, which mirrors the tiebreak rules.
Hint 2
Tuple comparison picks the smallest by default, so negate the quantities where larger is better (points, goal difference, goals scored) and keep the index as it is.
Hint 3
You only need two teams, not a full ordering. Find the best team, then find the best among the remaining indices (exclude by index). This takes linear time.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The ranking is a chain of criteria where each one only matters when all previous ones tie. That is exactly lexicographic tuple comparison, so we give every team a tuple key:
(-points, -goal_difference, -scored, index)
The first three quantities are "bigger is better", so we negate them and make "smaller key" mean "better team". The index is already "smaller is better", so it is left unchanged.
Then:
firstis the index with the minimum key.secondis the index with the minimum key among all indices exceptfirst.
Why it is correct
- Tuple comparison looks at the first position where two tuples differ. That matches the rule that a criterion is used only when all earlier ones are tied.
- Negation flips the direction correctly, including for negative goal differences. For example, a difference of
-3becomes3, which sorts after a difference of1(key-1). - The index is distinct for every team, so all keys are distinct and the minimum is unique.
- The second-ranked team beats everyone except the first-ranked team, so it is the minimum over everyone else. Excluding by index (not by key value) is the safe way to do this.
Using a weighted sum such as points * 1000 + goal_diff is a trap: goal difference can exceed the weight or be negative, so points would no longer always dominate. Tuples avoid this.
Complexity
- Time: O(n), two passes with a constant-size key per team.
- Space: O(1) extra, since keys are created on the fly.
Sorting everything would also be correct but costs O(n log n) time and O(n) space for only two results.
Reference solution
def top_two_clubs(wins, draws, scored, conceded):
n = len(wins)
def key(i):
# Smaller tuple means a better team.
return (
-(3 * wins[i] + draws[i]),
-(scored[i] - conceded[i]),
-scored[i],
i,
)
first = min(range(n), key=key)
second = min((i for i in range(n) if i != first), key=key)
return [first, second]