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. Nuxt框架实践

    前言 今天抽空过了遍nuxt文档,写了个实践demo,关于nuxt我已经断断续续看了好几遍了,自我感觉也算是入门了吧,从开发到上线心里都有底.后期打算在项目用起来的是nuxt框架,一些函数工具库,比如 ...

  2. Android Context讲解(转)

    博客出处 前言:本文是我读<Android内核剖析>第7章 后形成的读书笔记 ,在此向欲了解Android框架的书籍推荐此书. 大家好, 今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友 ...

  3. android studio 目录结构讲解

    android studio 目录结构讲解 src 毫无疑问,src目录是放置我们所有 Java代码的地方,它在这里的含义和普通 Java 项目下的 src目录是完全一样的,展开之后你将看到我们刚才创 ...

  4. 11个优秀的Android开发开源项目

    一. 一个类似微信的时光轴效果   时光轴效果 项目地址 https://github.com/ljtyzhr/TimeLine 二. 安卓选择器类库,包括日期.时间.单项.双项选择器.城市地址选择器 ...

  5. 如何在README.md文件中添加图片

    原文链接:https://www.cnblogs.com/hongmaju/p/6873720.html 1.在github上的仓库建立一个存放图片的文件夹,文件夹名字随意.如:img-folder ...

  6. Neutron控制节点集群

    #Neutron控制节点集群 openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #.Neutron控制节点集群 #本实 ...

  7. PHP如何强制下载文件

    很多网站都需要做文件下载的功能.如果直接给连接的方式下载的话有很多的弊处...因为有时候需要对下载权限的检查,对下载次数的检查.所以一般采用php的方法进行安全下载.但是下载的时候如果是txt jpg ...

  8. 自己实现String.prototype.trim方法

    今天呢 知乎看到一道题 说是网易面试题,要求自己写一个trim()方法, 实现 var str = "   a   sd  "; 去掉字符串两端的空格. 直接上码 var str ...

  9. 》》canvas时钟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. idea/eclipse下Maven工程集成web服务(tomcat、jetty)

     idea/eclipse下Maven工程集成web服务 转载请注明出处:http://www.cnblogs.com/funnyzpc/p/8093554.html 应用服务器最常用的一般有这哥仨: ...