506 Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".
Example 1:
Note:
N is a positive integer and won't exceed 10,000.
All the scores of athletes are guaranteed to be unique.
The Idea: Use index based hashing. First map the numbers to its index. Then you will be safe to sort the numbers. Initialize an empty arry of strings that are set to the size of the original input container. Next, iterate through the first 3 or less integers in the sorted array, and grab the index that cooresponds to the actual number. Set the value at this relative index to gold, silver, etc.. and use this same strategy to for the remainding ranks.
Complexity: O(N + NlogN) time and space
Last updated