Relative Ranks
Relative Ranks is LeetCode 506 (Easy). score[i] is the score of athlete i, and all scores are different. The highest score places 1st, the next places 2nd, and so on.
- Places 1, 2 and 3 get the labels
"Gold Medal","Silver Medal"and"Bronze Medal". - Every later place gets its number as a string, such as
"4".
Return the labels in the same order as score: answer[i] is athlete i's label.
- n == score.length
- 1 <= n <= 10^4
- 0 <= score[i] <= 10^6
- All the values in score are unique.
Intuition
A place comes from sorted order, but the answer must stay in input order. Sorting score itself breaks that link: after sorting you know the 2nd-best score but not whose it was.
So sort the athlete numbers instead of the scores, using each athlete's score as the sort key, highest first. Walking that list hands out places 1, 2, 3, … in order, and each label is written into answer[athlete]. The answer is filled out of order but read in input order, which is exactly what the problem asks for.
"Rank every item but report the ranks in the original order" means sorting indices by value and writing results back through those indices. The same move solves Rank Transform of an Array and any "replace each element by its rank" task.
Approach
Before reading on, work out how you would still know whose score is whose after sorting. Then decide where in the answer each label must go. Aim for O(n log n).
Sort the athlete numbers by score
Build order = sorted(range(n), key=lambda i: -score[i]). The minus sign sorts highest first, and because the list holds athlete numbers, each entry still says whose score it is.
Label each place
Walk order with its position place, counting from 0. Places 0, 1 and 2 take "Gold Medal", "Silver Medal" and "Bronze Medal" from a small list, and every later place takes its place number as a string, str(place + 1).
Write the label back to its athlete
Store the label in answer[order[place]]. That slot is the athlete's original position, so when the loop ends the answer lines up with score without any second pass.
Code in Python, C++ and Java
Common pitfalls
Sorting the scores instead of the athletes
ranked = sorted(score, reverse=True)
for place, s in enumerate(ranked):
answer[place] = label(place)order = sorted(range(n), key=lambda i: -score[i])
for place, i in enumerate(order):
answer[i] = label(place)Writing to answer[place] lists the labels in finishing order, not athlete order. For [10,3,8,9,4] it returns ["Gold Medal","Silver Medal","Bronze Medal","4","5"], but athlete 1 scored 3 and should get "5".
Off by one on the place number
answer[i] = str(place)
answer[i] = str(place + 1)
enumerate starts at 0, but places start at 1. The three medals hide the mistake, so it first shows on [5,4,3,2,1], where the 4th athlete comes out as "3".
Edge cases
With [7,9] only Gold and Silver are handed out: ["Silver Medal","Gold Medal"]. The loop stops after two places, so no extra check is needed.
Complexity
Relative Ranks FAQ
Can it be solved with a heap?
Yes. Push (-score[i], i) for every athlete into a min-heap and pop n times; the k-th pop is place k. It costs the same O(n log n) as sorting, so it only helps if you want to stop after the first few places.
Is there a faster way than sorting?
Scores are at most 10^6, so you can mark each score in an array of size 10^6 + 1 and scan it from the top, handing out places as you meet marked scores. That is O(n + max score) time but needs a million-slot array, so sorting is usually the better fit.