506. Relative Ranks
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的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- [LeetCode] 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] 506. Relative Ranks_Easy tag: Sort
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode Relative Ranks
原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...
随机推荐
- HTML基础下
知识点一: HTML5的标准结构: <!DOCTYPE html> <html lang='en'> <head> <meat charset='utf-8' ...
- day02HTML_CSS
掌握表单标签 <form action="http://www.baidu.com" method="post"> ... </form> ...
- 阿里云Https部署网站
0.开始之前 文章图片很多,注意流量 首先你得准备好一个已经备案成功的域名,并且有一个在阿里云的服务器部署了的网站. 然后就是你迫切的希望升级网站为HTTPS部署. 那么我们开始吧! 1.申请CA证书 ...
- AFNetworking提示3840 Unescaped control character around character XXX
处理办法:找到AFNetworking包中AFURLResponseSerialization.m文件在第250行修改代码如下: if (data.length > 0 && ! ...
- NSUserDefault
NSUserDefault是Cocoa提供的默认应用程序状态保持接口.它提供了简化的plist文件持久化方法.通过NSUserDefault类,你可以把用户首选项保存到plist文件中.到应用程序结束 ...
- Tomcat中虚拟路径
默认情况下,Tomcat访问静态资源配置是这样的 <Context path="/project_name" docBase="d:\tomcat_statics& ...
- 老男孩Python视频教程:第一周
认识和尝试Python 备注:老男孩Python视频教程,视频来自网络,在此分享,侵删 对我来说,第一周视频主要解答了以下疑问: 1. Python的三大特点是什么? 答:解释型.动态类型(运行期间才 ...
- Git版本控制器的使用
首先介绍一下什么是Git:git是目前最流行的版本控制系统,属于分布式版本控制器. 使用Git前先要在GitHub创建代码仓库,或者获取你要应用的GitHub的链接地址. 创建GitHub仓库这里就不 ...
- codeforces 893A Chess For Three 模拟
893A Chess For Three 思路: 直接模拟即可,第一盘永远是A与B开始 代码: #include <bits/stdc++.h> using namespace std; ...
- 在网站开发中经常用到的javaScript技术
1>屏蔽功能类 1.1 屏蔽键盘所有键<script language="javascript"><!--function document.onkeyd ...