原题链接在这里: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. iOS 点击注释图标 弹出对应解释

    需求:如题目  接上一篇的开发内容 效果图: 这种情况存在tableView 的一个cell中. 要点 1,  弹出的对应解释 要在可视区域,并且小尖角 要指着 图片 2,  文本不能过高 有极大高度 ...

  2. 教你在树莓派使用上RTC实时时钟,不用再担心断电后时间归零的问题,开机后自动同步RTC时钟!!!

    准备工作:1.系统建议使用官方最新的镜像文件 2.RTC时钟模块板(I2C接口)建议使用DS1307时钟模块,或者RTC时钟模块RTC时钟模块: 大家知道arduino的电平是5V,树莓派是3.3V, ...

  3. 数独C语言算法

    备好:http://blog.chinaunix.net/uid-26456800-id-3380612.html

  4. iOS_触摸事件与手势识别

    目  录: 一.触摸事件 1.1iOS的输入事件 1.2 触摸事件的处理 1.3 UITouch类中包含五个属性 1.4 UITouch类中包含两个成员函数 1.5响应者链 二.手势识别 2.1使用手 ...

  5. Windows命令行乱码问题解决

    命令 chcp功能: 显示或设置活动代码页编号 CHCP [nnn] nnn 指定代码页编号. 不加参数键入 CHCP 显示活动代码页编号. nnn指定一已有的系统字符集,该字符集在CONFIG.SY ...

  6. JAVA 中的集合框架

    java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 一.集合与数组 数组:(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知 ...

  7. Delphi_检查exe文件是否是"随机基址"

    ZC: cnpack 还是蛮好用的 1.代码: procedure TForm1.btnRandomizedBaseAddressClick(Sender: TObject); var pDosHdr ...

  8. Redis源码分析:serverCron - redis源码笔记

    [redis源码分析]http://blog.csdn.net/column/details/redis-source.html   Redis源代码重要目录 dict.c:也是很重要的两个文件,主要 ...

  9. 一道问题引出的python中可变数据类型与不可变数据类型

    一. 问题的提出 我们先来看两个对比 第一道题,当对象为整数时,最终结果:b = 2, a = 1,b的变化没有引起a的变化 a = 1 b = a b += 1 print(a) print(b) ...

  10. 【译】:lxml.etree官方文档

    本文翻译自:http://lxml.de/tutorial.html, 作者:Stefan Behnel 这是一个关于使用lxml.etree进行XML处理的教程.它简要介绍了ElementTree ...