Relative Ranks - Problem

๐Ÿƒโ€โ™‚๏ธ Olympic Competition Ranking System

You're tasked with implementing a ranking system for an Olympic competition! Given an array of athlete scores, you need to determine each athlete's rank and assign appropriate awards.

Here's how the ranking works:

  • ๐Ÿฅ‡ 1st place (highest score): "Gold Medal"
  • ๐Ÿฅˆ 2nd place: "Silver Medal"
  • ๐Ÿฅ‰ 3rd place: "Bronze Medal"
  • ๐Ÿƒ 4th place and beyond: Their numeric rank as a string (e.g., "4", "5", etc.)

Input: An integer array score where score[i] represents the score of the i-th athlete. All scores are guaranteed to be unique.

Output: An array answer where answer[i] is the rank/award of the i-th athlete.

Example: If scores are [10, 3, 8, 9, 4], the rankings would be: Gold Medal (10), 5th place (3), Bronze Medal (8), Silver Medal (9), 4th place (4).

Input & Output

example_1.py โ€” Standard Olympic Competition
$ Input: [5, 4, 3, 2, 1]
โ€บ Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
๐Ÿ’ก Note: The scores in descending order are [5, 4, 3, 2, 1]. So athlete with score 5 gets Gold Medal (1st), score 4 gets Silver Medal (2nd), score 3 gets Bronze Medal (3rd), score 2 gets rank "4", and score 1 gets rank "5".
example_2.py โ€” Mixed Score Order
$ Input: [10, 3, 8, 9, 4]
โ€บ Output: ["Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"]
๐Ÿ’ก Note: Sorted scores: [10, 9, 8, 4, 3]. Athlete 0 (score 10) gets Gold Medal, athlete 1 (score 3) gets rank "5", athlete 2 (score 8) gets Bronze Medal, athlete 3 (score 9) gets Silver Medal, athlete 4 (score 4) gets rank "4".
example_3.py โ€” Minimum Case
$ Input: [1]
โ€บ Output: ["Gold Medal"]
๐Ÿ’ก Note: With only one athlete, they automatically get the Gold Medal regardless of their score.

Constraints

  • n == score.length
  • 1 โ‰ค n โ‰ค 104
  • 0 โ‰ค score[i] โ‰ค 106
  • All the scores are guaranteed to be unique

Visualization

Tap to expand
๐Ÿ… Olympic Ranking SystemOriginal Athletes (Lane Order):10Lane 1๐Ÿƒโ€โ™‚๏ธ Alice3Lane 2๐Ÿƒโ€โ™€๏ธ Bob8Lane 3๐Ÿƒโ€โ™‚๏ธ Carol9Lane 4๐Ÿƒโ€โ™€๏ธ Dave4Lane 5๐Ÿƒโ€โ™‚๏ธ EveSorted Leaderboard:๐Ÿฅ‡ 10 (Alice)๐Ÿฅˆ 9 (Dave)๐Ÿฅ‰ 8 (Carol)4th: 4 (Eve)5th: 3 (Bob)Final Results (Original Order):Gold Medal5Bronze MedalSilver Medal4Time Complexity: O(n log n) for sorting + O(n) for lookup = O(n log n)Space Complexity: O(n) for hash map storing score-to-rank mappings๐Ÿ’ก Key InsightInstead of comparing eachscore with all others (O(nยฒ)),we sort once and create alookup table for instantrank retrieval!
Understanding the Visualization
1
Athletes Line Up
Athletes are in their original positions with their scores
2
Performance Ranking
Sort all scores to determine who gets which medal/rank
3
Create Scoreboard
Build a lookup table mapping each score to its award
4
Award Ceremony
Announce each athlete's award in their original order
Key Takeaway
๐ŸŽฏ Key Insight: Sort once to establish rankings, then use hash map for O(1) lookups - much better than O(nยฒ) repeated comparisons!
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen