码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 506. Relative Ranks_Easy tag: Sort

时间:2018-08-28 13:08:21      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:code   eve   EDA   first   fir   hle   integer   def   for   

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:

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:

  1. N is a positive integer and won‘t exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

 

Code

class Solution:
    def findRelativeRanks(self, nums):
        if len(nums) == 1: return ["Gold Medal"]
        if len(nums) == 2: return ["Gold Medal", "Silver Medal"] if nums[0] > nums[1] else ["Silver Medal", "Gold Medal"]
        ans, d = [0]*len(nums), sorted([(num, index) for index, num in enumerate(nums)], reverse = True)
        ans[d[0][1]], ans[d[1][1]], ans[d[2][1]] =  "Gold Medal", "Silver Medal", "Bronze Medal"
        for i in range(3, len(d)):
            ans[d[i][1]] = str(i+1)
        return ans

 

[LeetCode] 506. Relative Ranks_Easy tag: Sort

标签:code   eve   EDA   first   fir   hle   integer   def   for   

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9547446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!