原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description

题目:

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.

题解:

把nums sort同时要保留index信息. 按照index给result的对应index赋值.

Time Complexity: O(nlogn), n = nums.length. Space: O(n).

AC Java:

 public class Solution {
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 [] res = new String[nums.length];
for(int i = 0; i<nums.length; i++){
if(i == 0){
res[pair[i][1]] = "Gold Medal";
}else if(i == 1){
res[pair[i][1]] = "Silver Medal";
}else if(i == 2){
res[pair[i][1]] = "Bronze Medal";
}else{
res[pair[i][1]] = String.valueOf(i+1);
}
}
return res;
}
}

LeetCode Relative Ranks的更多相关文章

  1. [LeetCode] Relative Ranks 相对排名

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

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

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

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

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

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

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

  5. 【leetcode】506. Relative Ranks

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

  6. 506. 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算法题-Relative Ranks(Java实现)

    这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...

  9. 506 Relative Ranks 相对名次

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

随机推荐

  1. 预防SQL注入攻击

    /** * 预防SQL注入攻击 * @param string $value * @return string */ function check_input($value) { // 去除斜杠 if ...

  2. iOS 4.5.5版本 被拒绝!!!! "App Rejected : non-public APIs"

    今天上午收到邮件说是被拒绝了 原文是 这一版本 我就添加一个购买sku的方法, 并没有添加什么库 ,简简单单的一次升级给我出一私有方法拒绝!!!!! 在xcode8  iOS10 刚出来 ,苹果新规则 ...

  3. Adaptive Execution如何让Spark SQL更高效更好用

    1 背  景 Spark SQL / Catalyst 和 CBO 的优化,从查询本身与目标数据的特点的角度尽可能保证了最终生成的执行计划的高效性.但是 执行计划一旦生成,便不可更改,即使执行过程中发 ...

  4. 每天一个Linux命令(55)systemctl命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起.     (1)用法:     用法:  systemctl  [参数]  [服务 ...

  5. flex 实现图片播放 方案二 把临时3张图片预加载放入内存

    该方案,是预加载:前一张,当前,下一张图片,一共3张图片放入内存中.这样对内存的消耗可以非常小,加载之后的图片就释放内存. 下面示例一个是类ImagePlayers,一个是index.mxml pac ...

  6. nf_conntrack之解决方案

    1.现象 在/var/log/message中出现以下信息 Dec 8 11:22:29 product08 kernel: nf_conntrack: table full, dropping pa ...

  7. 主攻ASP.NET.4.5 MVC4.0之重生:二维码生成和谷歌二维码

    使用ThoughtWorks.QRCode.Codec 效果图 using ThoughtWorks.QRCode.Codec; 非原创代码 public void code(string id) { ...

  8. body-parser小解

    body-parser用来解析http请求体,对不同的content-type有不同的处理方式, 首先介绍一下常见的四种Content-Type: 1.application/x-www-form-u ...

  9. Linux VPS实用简单安全配置

    今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...

  10. 16个tomcat面试题

    1)解释什么是Jasper? Jasper是Tomcat的JSP引擎 它解析JSP文件,将它们编译成JAVA代码作为servlet 在运行时,Jasper允许自动检测JSP文件的更改并重新编译它们 2 ...