LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次
506. Relative Ranks
题目描述
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予“金牌”,“银牌”和“铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。
(注: 分数越高的选手,排名越靠前。)
每日一算法2019/6/11Day 39LeetCode506. Relative Ranks
示例 1:
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予“金牌”,“银牌”和“铜牌”("Gold Medal", "Silver Medal" and "Bronze Medal")。
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
- N 是一个正整数并且不会超过 10000。
- 所有运动员的成绩都不相同。
Java 实现
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class Solution {
public String[] findRelativeRanks(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
int n = nums.length;
String[] res = new String[n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(nums[i], i);
}
Arrays.sort(nums);
for (int i = n - 1; i >= 0; i--) {
String str = String.valueOf(n - i);
if (n - i == 1) {
str = "Gold Medal";
} else if (n - i == 2) {
str = "Silver Medal";
} else if (n - i == 3) {
str = "Bronze Medal";
}
res[map.get(nums[i])] = str;
}
return res;
}
}
参考资料
LeetCode 506. 相对名次(Relative Ranks) 39的更多相关文章
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- Java实现 LeetCode 506 相对名次
506. 相对名次 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌&q ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 【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 ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] Relative Ranks 相对排名
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 ...
- [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 ...
随机推荐
- 7-html列表
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 应用安全测试技术DAST、SAST、IAST对比分析【转】
转自:https://blog.csdn.net/qq_29277155/article/details/92411079 一.全球面临软件安全危机 2010年,大型社交网站rockyou.com被曝 ...
- Problem F. Wiki with String
Problem F. Wiki with StringInput file: standard input Time limit: 1 secondOutput file: standard outp ...
- 28-ESP8266 SDK开发基础入门篇--编写wifi模块TCP 客户端程序(官方API版,非RTOS版)
https://www.cnblogs.com/yangfengwu/p/11432795.html 注:这节实现的功能是WIFI模块作为TCP 客户端,连接咱的TCP服务器,然后实现透传 本来想着做 ...
- 【JZOJ6206】【20190610】二分图边染色
题目 对一个二分图的边染色,满足有相同端点的边的颜色一定不同; 设最优染色为\(C\) ,你的染色为\(X\),只需要满足$ X \le 2^ {\lceil log C \rceil }$ ...
- 洛谷P1560 蜗牛的旅行
题目 搜索,注意判断特殊情况,并且区分开什么时候转弯什么时候停止.然后转弯的时候更是要注意是否会进入障碍. #include <bits/stdc++.h> using namespace ...
- Hash算法解决冲突的四种方法
Hash算法解决冲突的方法一般有以下几种常用的解决方法 1, 开放定址法: 所谓的开放定址法就是一旦发生了冲突,就去寻找下一个空的散列地址,只要散列表足够大,空的散列地址总能找到,并将记录存入 公式为 ...
- go语言new和make
1.new func new(Type) *Type 内建函数,内建函数 new 用来分配内存,它的第一个参数是一个类型,它的返回值是一个指向新分配类型默认值的指针! 2.make func make ...
- [300iq contest1-J]Jealous Split
题意 有一个非负整数序列\({a_i}\),你要将他分成恰好\(k\)段,记\(s_i\)为第\(i\)段的和,\(m_i\)为第\(i\)段的最大值,你需要保证这种划分方案对任意\(1 \le i ...
- 数据仓库DW、ODS、DM概念及其区别
整体结构 在具体分析数据仓库之前先看下一下数据中心的整体架构以及数据流向 数据中心整体架构.png DB 是现有的数据来源,可以为mysql.SQLserver.文件日志等,为数据仓库提供数据来源 ...