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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code