LeetCode Relative Ranks
原题链接在这里: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:
- N is a positive integer and won't exceed 10,000.
- 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的更多相关文章
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- [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
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 506. 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算法题-Relative Ranks(Java实现)
这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
随机推荐
- python glob
http://python.jobbole.com/81552/ glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用 ...
- windows下查看静态库和动态库的导出函数
在window下查看动态库的导出函数可以用vs自带的Depends工具: 查看静态库的信息要用命令行来实现: dumpbin /LINKERMEMBER Test.lib > 1 ...
- Linux Shell编程 test命令
概述 test 命令是Shell 脚本中用来进行条件判断的. test命令示例 按照文件类型进行判断 测试选项 作 用 -b 文件 判断该文件是否存在,并且是否为块设备文件(是块设备文件为真) -c ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:政府行政网站常用友情链接跳转javascript[干货分享]
<!-----------------------------------> <script language="JavaScript" type="t ...
- Java 封装、继承、多态
Java中使用 extends 关键字 进行父类继承 在初始化子类时,子类会自动执行父类的构造方法, 如果子类的构造方法中没有显示调用父类的构造方法, 则系统会默认调用父类无参的构造方法 super( ...
- kubernetes 核心对象
Pods Pod是Kubernetes的基本操作单元,也是应用运行的载体.整个Kubernetes系统都是围绕着Pod展开的,比如如何部署运行Pod.如何保证Pod的数量.如何访问Pod等.另外,Po ...
- 吴恩达深度学习笔记(七) —— Batch Normalization
主要内容: 一.Batch Norm简介 二.归一化网络的激活函数 三.Batch Norm拟合进神经网络 四.测试时的Batch Norm 一.Batch Norm简介 1.在机器学习中,我们一般会 ...
- CDN技术介绍
CDN技术介绍 一.CDN概述 1.1 CDN定义 CDN即Content Delivery Network (内容分发网络).CDN是建立在现有IP网络基础结构之上的一种增值网络.是在应用层部署的一 ...
- 字符串问题之 去掉字符串中连续出现K个0的子串
字符串中刚好出现K个连续的‘O’,则把K个连续‘O’字符去除,返回处理后的字符串 比如 str="AOOOOOBOOO" k=3, 返回“AOOOOOB” 这个题的解决思路也有 ...
- JavaWeb -- 内省—beanutils工具包 的使用
Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写. Beanu ...