Problem Description
Given an array of unique scores for athletes, determine each athlete's rank based on their score. The highest score gets "Gold Medal", the second-highest "Silver Medal", the third-highest "Bronze Medal", and the rest receive their numerical placement as a string.
Key Insights
- We need to maintain the original indices while sorting the scores.
- Sorting the score array in descending order helps determine each athlete's placement.
- Special handling for the top three placements with medal names.
- Use an additional array to store the final ranking based on original indices.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting of the scores. Space Complexity: O(n) for storing indexed scores and the result array.
Solution
The approach is to pair each score with its original index and then sort the list in descending order based on scores. Once sorted, iterate through the list to assign appropriate ranks:
- For the first, second, and third positions, assign "Gold Medal", "Silver Medal", and "Bronze Medal" respectively.
- For all other positions, assign the string value of the current placement index (1-indexed). Finally, place the assigned rank into the result array at the correct original index.