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的更多相关文章

  1. 【leetcode】506. Relative Ranks

    problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...

  2. 【LeetCode】506. Relative Ranks 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...

  3. 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  4. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  5. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  6. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  7. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  8. [LeetCode&Python] Problem 520. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

  9. [LeetCode&Python] Problem 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

随机推荐

  1. ActiveMQ 事务和XA

    1. 客户端怎样显式地使用事务? producer 开启事务(代码片段): ActiveMQSession session = (ActiveMQSession)connection.createSe ...

  2. ActiveMQ Message Groups

    http://activemq.apache.org/message-groups.html 与Exclusive Consumer相比,Message Groups的对消息分组的粒度更细.具有相同g ...

  3. SecureCRT sftp上传文件报错:put: failed to upload xxx 拒绝访问

    1.问题 使用sftp上传文件时报错:put: failed to upload xxx 拒绝访问.类似下图所示: 2.原因 造成这个问题的原因可能有两个,一是要上到的那个目录剩余磁盘空间不足,二是打 ...

  4. WeRun is mini-app

    WeRun is a mini-app within WeChat that allows users to monitor their daily step count. One of its mo ...

  5. error: http://ppa.launchpad.net lucid Release: The following signatures couldn't be verified because

    ubuntu 命令行sudo apt-get update W: GPG error: http://ppa.launchpad.net lucid Release: The following si ...

  6. set循环遍历删除特定元素

    使用Iterator迭代器 public class Demo { public static void main(String[] args) { Set<Object> obj = n ...

  7. DevExpress v18.1新版亮点——Reporting篇(四)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Reporting v18.1 的新功能,快来下载试用新版本 ...

  8. poj3080(kmp+枚举)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20163   Accepted: 8948 Descr ...

  9. L259

    Few things can feel as crushing as being rejected by someone who you're either dating or romanticall ...

  10. LibHaru编译使用

    最近公司准备在PDF方面发力了,我也要行动起来,就找到了LibHaru这个开源库 编译Libharu需要用到zlib库和libpng库,libpng库又依赖zlib库. zlib 下载地址:http: ...