[LeetCode&Python] Problem 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:
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]
"""
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的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 前端框架之Vue.js
前言: 前端主流框架有Vue.react.angular,目前比较火因为Vue比较容易学习,运营起来比较快速: Vue是什么呢? 是一个基于MVVM架构的,前端框架: 如果你之前已经习惯了用jQuer ...
- Oracle Linux下载教程(以Oracle Linux 6.9为例)
一.说明 Oracle Linux是Oracle基于Redhat,针对Oracle系列软件(尤其是Oracle数据库)进行优化后的Linux发行版. 虽然Oracle Linux下载是免费的,但是并不 ...
- wps去除首字母自动大写
首字母大写功能在不是进行英文编写时是个“自作聪明”的功能,我们可能会想把它关掉.
- Vuex的深入学习
1.vuex 的dispatch和commit提交mutation的区别 (1)当你的操作行为中含有异步操作,比如向后台发送请求获取数据,就需要使用action的dispatch去完成了.其他使用co ...
- 学习笔记-AngularJs(三)
学习笔记-AngularJs(二)写了个所有程序语言入门时都必须要写的Hello World,那么从现在开始做那个之前说过的互联网大佬介绍的学习例子,当然这里开始会慢慢按照之前说过的目录来搭建这个学习 ...
- H5离线缓存技术Application Cache
H5离线缓存技术Application Cache 1.离线缓存技术:是浏览器本身的一种机制 HTML5引入Application Cache(应用程序缓存)技术,离线存储可以将站点的一些文件存储在本 ...
- 2-Servlet和servletContext
2018-08-09 22:34 * Servlet(好好学) * 动态WEB的资源. * 什么是Servlet * 实现Servlet接口,重写5个方法. * S ...
- java倒计时使用java.util.Timer实现,使用两个线程,以秒为单位
public class Countdown3 { private int lin; private int curSec; public Countdown3(int lin)throws Inte ...
- linux 用户/群组/权限
mv 原文件名 新文件名 #相当于重命名 查看文件内容相关命令 cat #查看文件全部内容 head - n #查看文件前n行内容(默认前十行) tail -n #查看文件后n行内容(默认后十行) t ...
- day1 计算机硬件基础
CPU包括运算符和逻辑符 储存器包括内存和硬盘 7200转的机械硬盘一般找到想要的数据需要9毫秒的时间 4+5 5毫秒的时间是磁头到磁盘轨道 4毫秒是平均开始查找想要的数据到找到的 ...