The problem is about assigning relative ranks to athletes based on their scores. You are given a list of integers representing the scores of athletes. Your task is to return a list of strings where each string describes the rank of the athlete. The ranks are assigned in descending order of the scores:
Here’s the complete Python code for this problem:
def findRelativeRanks(score):
# Prepare list of tuples (score, original_index)
ranked_scores = [(s, i) for i, s in enumerate(score)]
# Sort the list based on scores in descending order
ranked_scores.sort(reverse=True, key=lambda x: x[0])
# Initialize result list with empty strings
result = [""] * len(score)
# Assign ranks based on sorted order
for rank, (s, i) in enumerate(ranked_scores):
if rank == 0:
result[i] = "Gold Medal"
elif rank == 1:
result[i] = "Silver Medal"
elif rank == 2:
result[i] = "Bronze Medal"
else:
result[i] = str(rank + 1)
return result
# Example usage:
scores = [10, 3, 8, 9, 4]
print(findRelativeRanks(scores))
# Output: ["Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"]
This code effectively assigns the ranks and ensures the output retains the order of the original scores.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?