You are given an integer array score of size n, where score[i] is the score of the ith 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:
1st place athlete's rank is "Gold Medal".2nd place athlete's rank is "Silver Medal".3rd place athlete's rank is "Bronze Medal".4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x").Return an array answer of size n where answer[i] is the rank of the ith athlete.
Example 1:
Input: score = [5,4,3,2,1] Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"] Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].
Example 2:
Input: score = [10,3,8,9,4] Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"] Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
Constraints:
n == score.length1 <= n <= 1040 <= score[i] <= 106score are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To figure out the relative ranks, we can check every possible ordering of the scores. We will go through each score and compare it with every other score to determine its rank and give it the appropriate medal or rank number.
Here's how the algorithm would work step-by-step:
def relative_ranks_brute_force(scores):
result = []
for current_score in scores:
rank = 1
# Determine the rank by comparing with other scores
for other_score in scores:
if other_score > current_score:
rank += 1
# Assign medals based on rank
if rank == 1:
result.append("Gold Medal")
elif rank == 2:
result.append("Silver Medal")
elif rank == 3:
result.append("Bronze Medal")
else:
result.append(str(rank))
return resultThe key idea is to figure out who the top three performers are first. Then, instead of ranking everyone individually, we only need to handle the top three specially and rank the rest numerically.
Here's how the algorithm would work step-by-step:
def relative_ranks(scores):
copied_scores = sorted(scores, reverse=True)
# Store the rank for each score to easily retrieve later.
score_to_rank = {}
rankings = [""] * len(scores)
for i, score in enumerate(copied_scores):
if i == 0:
score_to_rank[score] = "Gold Medal"
elif i == 1:
score_to_rank[score] = "Silver Medal"
elif i == 2:
score_to_rank[score] = "Bronze Medal"
else:
score_to_rank[score] = str(i + 1)
# Assign the correct rank to the original scores
for i, score in enumerate(scores):
rankings[i] = score_to_rank[score]
return rankings| Case | How to Handle |
|---|---|
| Empty or null input array | Return an empty array to indicate no rankings are possible. |
| Input array with only one element | Return an array with a single element 'Gold Medal'. |
| Input array with two elements | Return an array containing either 'Gold Medal', 'Silver Medal' or 'Silver Medal', 'Gold Medal' based on relative values. |
| Input array with three elements | Handle the Gold, Silver, and Bronze medals assignment correctly based on their relative order. |
| Input array with a large number of elements (performance scaling) | Ensure the sorting algorithm used has good average-case time complexity (e.g., O(n log n)). |
| Input array contains duplicate scores | The ranking logic should assign the same rank to duplicate scores, following the problem description of the 4th highest score receiving rank '4'. |
| Input array contains very large score values (integer overflow) | Use appropriate data types (e.g., long) to prevent integer overflow during calculations or comparisons. |
| Input array contains all identical score values | Assign the same rank (rank '4') to all athletes after assigning the top 3 ranks as best as possible |