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

  1. 【leetcode】506. Relative Ranks

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

  2. [LeetCode&Python] Problem 506. Relative Ranks

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

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

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

  4. 506 Relative Ranks 相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...

  5. LeetCode 506. 相对名次(Relative Ranks) 39

    506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...

  6. [LeetCode] Relative Ranks 相对排名

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

  7. [Swift]LeetCode506. 相对名次 | Relative Ranks

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

  8. [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 ...

  9. LeetCode Relative Ranks

    原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...

随机推荐

  1. android 圆角 ImageView

    android中Imageview 内的图片圆角的实现方式 此文针对的是 imageview中图片的圆角, 即忽略掉图片的ScaleType, 均对图片采取圆角.  而不是对Imageview本身的圆 ...

  2. Android开发之漫漫长途 Ⅵ——图解Android事件分发机制(深入底层源码)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  3. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  4. eclipse中 web项目缺少tomcatl lib的解决办法

    1.最近在搭建的项目中,将项目导入eclipse中突然报好多错误,查看后全是丢失tomcat的lib包的错误,莫名其妙的错误. 代码中缺少的也是这样的问题 很明显,我之前的包丢了,莫名其妙的丢了. 解 ...

  5. 中介者模式(Mediator)

    中介者模式(Mediator) 中介者模式也是用来降低类类之间的耦合的,因为如果类类之间有依赖关系的话,不利于功能的拓展和维护,因为只要修改一个对象,其它关联的对象都得进行修改.如果使用中介者模式,只 ...

  6. php接入支付宝的流程

    php接入支付宝的流程写在这里供像我一样的小白参考. 1.首先要有一个创建一个应用(选好自己想要的功能,关于支付的功能,貌似都需要签约) 2.下载SDK&Dome(网址https://doc. ...

  7. PHP设置环境变量

    如果提示php命令不存在.说明未设置.设置方法如下 方法一:直接运行命令 [PHP] 纯文本查看 复制代码 ? 1 export PATH=$PATH:/usr/local/xxxx/php/bin ...

  8. 详解spl_autoload_register()函数

    一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.class.php    <?php    class  ...

  9. onblur

    HTML onblur 事件属性 HTML 事件属性 实例 当用户离开输入字段时对其进行验证: <input type="text" name="fname&quo ...

  10. log4j2 项目日志组件

    在项目运行过程中,常常需要进行功能调试以及用户行为的跟踪和记录,部分人习惯使用System.out,但这并不建议,它仅仅是使用方便但不便于维护也无扩展性.相比log4j的话,log4j可以控制日志信息 ...