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:
- 1.N is a positive integer and won't exceed 10,000.
- 2.All the scores of athletes are guaranteed to be unique.
这道题目的意思是返回数组中每一个元素的排名,不能直接排序这样会打乱每一个元素的原始位置,因为题目上面说数组中的每一个值都是唯一的,所以可以用python的字典方面的找出排序。
这里我使用的是leetcode其他用户给出的java写法,主要是因为里面的lambda新特性。
public String[] findRelativeRanks(int[] nums) {
int pair[][] = new int[nums.length][2];
for(int i = 0; i < nums.length; i++)
{
pair[i][0] = nums[i];
pair[i][1] = i;
}
Arrays.sort(pair,(a,b) -> (b[0] - a[0]));
String result[] = new String[nums.length];
for(int i = 0; i < nums.length; i++)
{
if (i == 0)
result[pair[i][1]] = "Gold Medal";
else if (i == 1)
result[pair[i][1]] = "Silver Medal";
else if (i == 2)
result[pair[i][1]] = "Bronze Medal";
else
result[pair[i][1]] = (i + 1) + "";
}
return result;
}
上面连接已经给出了详解,我在啰嗦一下pair数值发生变化
10, 0
3, 1
8, 2
9, 3
4, 4
排序后
10, 0
9, 3
8, 2
4, 4
3, 1
详细的lambda介绍可见这里
506. Relative Ranks的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] 506. Relative Ranks_Easy tag: Sort
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode Relative Ranks
原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...
随机推荐
- laravel5.3统计 withCount()方法的使用
在laravel5.3之后可以使用withCount()这个方法. 注意:一定要是5.3版本之后,5.2和5.1都会报方法未定义 举个栗子: App\Post::withCount('comments ...
- java如何声明一个数组用来存储随机生成的字母并且保证不重复
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px ...
- ShoneSharp语言(S#)的设计和使用介绍系列(4)— 入门概述
ShoneSharp语言(S#)的设计和使用介绍 系列(4)- 入门概述 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. ...
- 责任链模式(Chain of Responsibility)
责任链模式(Chain of Responsibility)接下来我们将要谈谈责任链模式,有多个对象,每个对象持有对下一个对象的引用,这样就会形成一条链,请求在这条链上传递,直到某一对象决定处理该请求 ...
- 垃圾陷阱洛谷dp
题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2<=D<=100)英尺. 卡门想把垃圾堆起来,等到 ...
- JavaSE之绘制菱形
在JavaSE的算法练习中,绘制菱形是一个比较常见的案例.菱形效果如下图所示: 我们在解决算法问题时,通常情况下,先不要急于马上编码,而是要先观察,找出解决问题的关键所在. 在上图中,我们可以看到,菱 ...
- php银联网页支付实现方法
本文实例讲述了php银联网页支付实现方法.分享给大家供大家参考.具体分析如下: 这里介绍的银联WAP支付功能,仅限消费功能. 1. PHP代码如下: 复制代码代码如下: <?phpna ...
- 【福利】十一起,小冰科技所有UWP产品免费半个月
从十月一日起(UTC协调世界时),至十月十五,小冰科技所有UWP产品免费半个月!!!!!! 注意是UTC哦,中国区,比UTC早8个小时,要等到十月一号早晨八点开始... 现在小冰科技旗下一共发布了 5 ...
- linux上kafka模拟客户端发送、接受消息
producer 消息的生成者,即发布消息 consumer 消息的消费者,即订阅消息 broker Kafka以集群的方式运行,可以由一个或多个服务组成,服务即broker zook ...
- Linux 账号管理与 ACL 权限配置
要登陆 Linux 系统一定要有账号与口令才行,否则怎么登陆,您说是吧?不过, 不同的使用者应该要拥有不同的权限才行吧?我们还可以透过 user/group 的特殊权限配置, 来规范出不同的群组开发项 ...