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.length
1 <= n <= 104
0 <= score[i] <= 106
score
are unique.This problem requires us to determine the ranks of athletes based on their scores and return them in an array. The ranks follow a specific pattern: 1st place is "Gold Medal", 2nd is "Silver Medal", 3rd is "Bronze Medal", and subsequent places are represented by their numerical rank (e.g., 4th place is "4").
The most efficient approach involves sorting the scores while keeping track of the original indices of the athletes. This allows us to map the sorted ranks back to the original order. We'll use an array of indices to maintain this mapping.
Algorithm:
Create Index Array: Generate an array idx
containing the indices 0 to n-1, where n is the length of the score
array. Each index represents the position of a score in the original input.
Sort Indices: Sort the idx
array based on the corresponding scores in the score
array in descending order. This ensures that the highest score is at the beginning of the sorted idx
array. Many languages allow custom sort functions to achieve this.
Rank Assignment: Create a result
array to store the ranks (strings). Iterate through the sorted idx
array:
Map Ranks to Original Order: Use the original indices from idx
to place the ranks correctly in the result
array, reflecting the original order of athletes in the score
array.
Return Result: Return the result
array containing the string ranks.
idx
array and the result
array.class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
n = len(score)
idx = list(range(n)) # Create index array
# Sort indices by score in descending order
idx.sort(key=lambda i: score[i], reverse=True)
ranks = ["Gold Medal", "Silver Medal", "Bronze Medal"] + [str(i + 4) for i in range(n - 3)]
result = [""] * n # Initialize result array
for i, index in enumerate(idx): # Map ranks to original order
result[index] = ranks[i]
return result
The other languages (Java, C++, Go, TypeScript) follow a very similar structure, with the primary difference lying in the syntax and built-in functions for sorting and string manipulation. The core algorithm remains the same. The provided code examples in the question illustrate these variations effectively.