码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode&Python] Problem 506. Relative Ranks

时间:2018-12-06 10:16:32      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:lan   def   app   got   [1]   with   out   ati   rank   

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.
class Solution(object):
    def findRelativeRanks(self, nums):
        """
        :type nums: List[int]
        :rtype: List[str]
        """
        n=len(nums)
        if n==0:
            return nums
        elif n==1:
            return [‘Gold Medal‘]
        elif n==2:
            if nums[0]>nums[1]:
                return [‘Gold Medal‘,‘Silver Medal‘]
            else:
                return [‘Silver Medal‘,‘Gold Medal‘]
        ans=[]
        s=sorted(nums)
        a1=s[-1]
        a2=s[-2]
        a3=s[-3]

        for i in nums:
            if i==a1:
                ans.append(‘Gold Medal‘)
            elif i==a2:
                ans.append(‘Silver Medal‘)
            elif i==a3:
                ans.append(‘Bronze Medal‘)
            else:
                ans.append(str(n-s.index(i)))
        return ans

  

[LeetCode&Python] Problem 506. Relative Ranks

标签:lan   def   app   got   [1]   with   out   ati   rank   

原文地址:https://www.cnblogs.com/chiyeung/p/10074727.html

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