[LeetCode] 506. Relative Ranks_Easy tag: Sort
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.
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的更多相关文章
- [LeetCode] 455. Assign Cookies_Easy tag: Sort
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- [LeetCode] 252. Meeting Rooms_Easy tag: Sort
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- [LeetCode] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
随机推荐
- Workbox 缓存
介绍 https://developers.google.cn/web/tools/workbox/guides/get-started 先注册一个service worker <script& ...
- sublime3 配置go的开发环境
配置go的环境变量:https://studygolang.com/articles/4910 配置sublime:https://studygolang.com/articles/4938
- svn checkout 指定目录(转)
http://www.uqugu.com/blog/article/svn-checkout-specified-forder/ svn有时只想检出指定目录,对于其他的大文件目录则不想检出,如不想检出 ...
- c# 编程小技巧
1.对于界面布局,可以考虑使用 wpf,对于传统winfrom来说,tableLayoutPanel1可能是最好的选择. 2.你一定会问,如何使用代码管理大量的按钮,可以使用 List<Butt ...
- 【鬼畜】UVA - 401每日一题·猛男就是要暴力打表
管他什么rev数组,msg数组简化代码 #define _CRT_SECURE_NO_WARNINGS #include <cmath> #include <iostream> ...
- 读书笔记-iOS核心动画高级技巧
如果不使用+imageNamed:,那么把整张图片绘制到CGContext可能是最佳的方式了. 这里我们利用了CALayer的KVC来存储和检索任意的值,将图层和索引打标签. 使用KVC打标签
- AndroidStudio_ListView
在这里梳理一下ListView的用法: 1.建立一个activity,例如建立一个ListViewActivity,这时将生成两个文件:ListViewActivity.java和activity_l ...
- SpringMVC 的使用映射路径 <mvc:resources >
以下是测试结果,可能存在纰漏,暂记录一下. 使用springMVC时,一般将DispatcherServlet请求映射配置为"/",则Spring MVC将捕获Web容器所有的请求 ...
- SQL[Err]ORA-00XXX: missing 相关
1.[Err]ORA-00936: missing expression 造成这个错误的原因是:选取的最后一个字段与from之间有逗号 解决方法:将字段与from之间的逗号去掉. 2.[Err] OR ...
- Excel--数据对比方法
1.函数对比: 适用于两列数据对比 =IF(EXACT(A2,B2)=TRUE,"相同","不同") 2.快捷键对比: 适用于少数数据对比 选中对比两列数据,快 ...