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:
1 2 3 4 5
Input: [5, 4, 3, 2, 1] Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"] Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
N is a positive integer and won’t exceed 10,000.
All the scores of athletes are guaranteed to be unique.
class Solution(object): def findRelativeRanks(self, nums): """ :type nums: List[int] :rtype: List[str] """ numsStrs = [] for i in range(len(nums)): numsStrs.append(str(nums[i])+'-'+str(i))
numsStrs = sorted(numsStrs, lambda x,y: cmp(int(x.split("-")[0]), int(y.split("-")[0])))[::-1] ranks = [''] * len(nums) for i in range(len(numsStrs)): index = int(numsStrs[i].split("-")[1]) if i == 0: ranks[index] = "Gold Medal" elif i == 1: ranks[index] = "Silver Medal" elif i == 2: ranks[index] = "Bronze Medal" else: ranks[index] = str(i+1) return ranks