Relative Ranks - Problem
You are given an integer array score of size n, where score[i] is the score of the i-th athlete in a competition. All the scores are guaranteed to be unique.
The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
- The 1st place athlete's rank is
"Gold Medal" - The 2nd place athlete's rank is
"Silver Medal" - The 3rd place athlete's rank is
"Bronze Medal" - For the 4th place to the nth place athlete, their rank is their placement number (i.e., the x-th place athlete's rank is
"x")
Return an array answer of size n where answer[i] is the rank of the i-th athlete.
Input & Output
Example 1 — Basic Ranking
$
Input:
score = [5,4,3,2,1]
›
Output:
["Gold Medal","Silver Medal","Bronze Medal","4","5"]
💡 Note:
Score 5 is highest (Gold), 4 is second (Silver), 3 is third (Bronze), 2 gets rank "4", 1 gets rank "5"
Example 2 — Unordered Scores
$
Input:
score = [10,3,8,9,4]
›
Output:
["Gold Medal","5","Silver Medal","Bronze Medal","4"]
💡 Note:
10 is highest (Gold at index 0), 9 is second (Bronze at index 3), 8 is third (Silver at index 2), 4 gets "4", 3 gets "5"
Example 3 — Minimum Size
$
Input:
score = [1]
›
Output:
["Gold Medal"]
💡 Note:
Single athlete automatically gets first place and Gold Medal
Constraints
- 1 ≤ score.length ≤ 104
- 0 ≤ score[i] ≤ 106
- All values in score are unique
Visualization
Tap to expand
Understanding the Visualization
1
Input Scores
Array of athlete scores in original order
2
Rank by Performance
Determine placement: highest=1st, second=2nd, etc.
3
Assign Awards
Convert ranks to medals (top 3) or numbers (4th+)
Key Takeaway
🎯 Key Insight: Sort scores with index tracking to efficiently assign ranks while preserving original order
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code