Leaderboard Ranking

Medium · rating 1300 · sorting

The first line contains n. Each of the next n lines has a player name and their score, separated by a space. Print the player names one per line, ordered by score from highest to lowest, breaking ties by name in ascending alphabetical order.

Constraints: 1 ≤ n ≤ 105

Editorial

Leaderboard Ranking orders players by score (highest first), breaking ties alphabetically — a custom multi-key sort.

Approach

Sort by a composite key: descending score, then ascending name. Most languages let you sort by a tuple such as (-score, name).

players.sort(key=lambda p: (-p.score, p.name))
for p in players:
    print(p.name)

Complexity

Time: O(n log n). Space: O(n).

Open Leaderboard Ranking in Code Arena →