Aggregate Tournament Standings from User Rankings
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests pairwise comparison aggregation, majority voting with tie-breaking, and ranking by win totals.
You receive ranked lists from n users evaluating m items, each labeled 0 through m - 1.
Each user i provides an array rankings[i] containing all m item IDs in their preferred order. Earlier positions indicate stronger preference: if item a appears before item b in rankings[i], user i prefers a to b.
Build an overall ranking using these rules:
Head-to-head comparisons:
For any two distinct items x and y, determine which one wins their direct matchup:
- Count how many users prefer
xovery(call thisvotes_for_x) - Item
xdefeats itemywhen either:votes_for_xexceeds half the total users (strict majority:votes_for_x > n / 2), ORvotes_for_xequals exactly half (tie in votes:votes_for_x * 2 == n) ANDxhas a smaller ID thany
Overall ranking:
Each item's strength is measured by its victory count: the number of other items it defeats in head-to-head matchups.
Item x ranks higher than item y when:
xhas more victories thany, OR- They have equal victories and
xhas a smaller ID
Return all m item IDs ordered from strongest to weakest according to this ranking.
Constraints:
1 <= n <= 10001 <= m <= 100- Each
rankings[i]is a valid permutation of[0, 1, ..., m-1]
Examples
Example 1
Input: compute_ranked_items(3, 4, [[0, 1, 2, 3], [0, 2, 1, 3], [1, 0, 2, 3]])
Output: [0, 1, 2, 3]
Example 2
Input: compute_ranked_items(2, 3, [[0, 1, 2], [2, 1, 0]])
Output: [0, 1, 2]
Example 3
Input: compute_ranked_items(1, 5, [[4, 3, 2, 1, 0]])
Output: [4, 3, 2, 1, 0]
Hints
Hint 1
Build a matrix tracking how many users prefer each item over each other item by examining positions in the ranking arrays.
Hint 2
Use integer arithmetic (multiply by 2) when checking for majority to avoid floating-point precision issues.
Hint 3
An item's victory count can be computed by checking all possible opponents and applying the defeat rules consistently.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem models a round-robin tournament where every pair of items competes once, with users acting as judges.
Step 1: Count pairwise preferences
Build an m × m matrix where preference[a][b] stores how many users rank item a above item b. For each user's ranking, every item at position i is preferred over all items at positions j > i. We increment preference[ranking[i]][ranking[j]] for each such pair.
Step 2: Determine victories
For each ordered pair (x, y) where x ≠ y, item x defeats item y based on these conditions:
- Strict majority:
preference[x][y] > n/2, checked aspreference[x][y] * 2 > nto avoid floating-point arithmetic - Vote tie with ID tiebreaker:
preference[x][y] * 2 == nandx < y
Count how many items each item defeats to get its victory total.
Step 3: Sort by victory totals
Order items by the composite key (-victories[item], item). Higher victory counts come first; smaller IDs break ties.
Why it works
The preference matrix accurately captures user preferences because we process every pair of positions in each ranking exactly once. Since every user ranks one item above the other, preference[x][y] + preference[y][x] = n for any pair. This means exactly one item wins each matchup: either one has a strict majority, or the votes split evenly and the smaller ID wins.
Victory counts measure overall strength. Sorting by these counts with ID tiebreaking produces a deterministic total ordering that respects the problem's definition of "ranks higher than."
Complexity
Time: O(n·m² + m²) = O(n·m²)
- Building the preference matrix requires examining m(m-1)/2 pairs for each of n users
- Computing victories checks m² pairs
- Sorting m items takes O(m log m)
- The first term dominates
Space: O(m²) for the preference matrix, plus O(m) for victory counts and output
from typing import List
def compute_ranked_items(n: int, m: int, rankings: List[List[int]]) -> List[int]:
# preference[a][b] = count of users who rank item a higher than item b
preference = [[0] * m for _ in range(m)]
for user_ranking in rankings:
for i in range(m):
item_at_i = user_ranking[i]
for j in range(i + 1, m):
item_at_j = user_ranking[j]
preference[item_at_i][item_at_j] += 1
# victories[x] = number of items that x defeats
victories = [0] * m
for x in range(m):
for y in range(m):
if x == y:
continue
# x defeats y if strict majority or tie with smaller ID
if preference[x][y] * 2 > n or (preference[x][y] * 2 == n and x < y):
victories[x] += 1
# Sort by victories descending, then by ID ascending
return sorted(range(m), key=lambda item: (-victories[item], item))