【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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:
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.
Output:
7
Note:
- N is a positive integer and won’t exceed 10,000.
- All the scores of athletes are guaranteed to be unique.
题目大意
把一个数组中最大的三个位置设置成金银铜奖,其他位置是当前数字的排名。
解题方法
排序
java也可以通过sort的方法,把类似键值对的数组按照第一维排序,第二维也会跟着排序:
Example:
nums[i] : [10, 3, 8, 9, 4]
pair[i][0] : [10, 3, 8, 9, 4]
pair[i][1] : [ 0, 1, 2, 3, 4]
After sort:
pair[i][0] : [10, 9, 8, 4, 3]
pair[i][2] : [ 0, 3, 2, 4, 1]
这样就可以找出前几个较大值对应的序号,从而标出名词。
参考这个详细解答:https://discuss.leetcode.com/topic/77876/easy-java-solution-sorting
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][4] = i;
}
Arrays.sort(pair, (a, b) -> (b[0] - a[0]));
String[] ans = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
ans[pair[i][5]] = "Gold Medal";
} else if (i == 1) {
ans[pair[i][6]] = "Silver Medal";
} else if (i == 2) {
ans[pair[i][7]] = "Bronze Medal";
} else {
ans[pair[i][8]] = "" + (i + 1);
}
}
return ans;
}
}
argsort
这个题,因为我用python做kNN的时候也要找出前几个大值所在的序号,就用了numPy的argsort()函数。LeetCode也可以用Numpy的!!看我的解法!!
import numpy as np
class Solution:
def findRelativeRanks(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
ranks = np.argsort(np.array(nums))[::-1]
N = len(nums)
res = list(map(str, ranks))
for r in range(N):
if r == 0:
res[ranks[0]] = "Gold Medal"
elif r == 1:
res[ranks[1]] = "Silver Medal"
elif r == 2:
res[ranks[2]] = "Bronze Medal"
else:
res[ranks[r]] = str(r + 1)
return res
堆
import numpy as np
class Solution:
def findRelativeRanks(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
heap = [(-num, i) for i, num in enumerate(nums)]
heapq.heapify(heap)
N = len(nums)
res = [""] * N
count = 1
while heap:
num, i = heapq.heappop(heap)
if count == 1:
res[i] = "Gold Medal"
elif count == 2:
res[i] = "Silver Medal"
elif count == 3:
res[i] = "Bronze Medal"
else:
res[i] = str(count)
count += 1
return res
日期
2017 年 4 月 14 日
2018 年 11 月 16 日 —— 又到周五了!
【LeetCode】506. Relative Ranks 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
随机推荐
- LVS-三种模式的配置详情
NAT模式 实验环境 LVS1 VIP 192.168.31.66 DIP 192.168.121.128 WEB1 192.168.121.129 WEB2 192.168.121.130 安装与配 ...
- 34. Swap Nodes in Pairs
Swap Nodes in Pairs My Submissions QuestionEditorial Solution Total Accepted: 95230 Total Submission ...
- UE4打包启动失败:RunUAT.bat ERROR: AutomationTool failed to compile.
打包配置正常的情况下,出现下面Log: RunUAT.bat ERROR: AutomationTool failed to compile. 基本上,可以先排查下任务管理器中是不是有UE4Edito ...
- 关于Stream的使用
引言 Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找.过滤和映射数据等操作.使用Stream API 对集合数据进行操作,就类似于使用 ...
- Hive(七)【内置函数】
目录 一.系统内置函数 1.查看系统自带内置函数 2.查看函数的具体用法 二.常用内置函数 1.数学函数 round 2.字符函数 split concat concat_ws lower,upper ...
- Android项目的settings.gradle和build.gradle
gradle构建的项目中的build.gradle和settings.gradle文件 build.gradle 浅析(一) 史上最全的Android build.gradle配置教程 Android ...
- notepad++ 连接远程服务器
前言:为了便于编辑 linux 上的文件,因此通过 notepad++ 连接服务器后打开,编辑完,保存即可 1. 打开 notepad++,安装插件 2. 搜索 NppFtp,找到后 点击 安装/in ...
- 【JavaWeb】【JSP】JSP传值到Servlet后端为NULL的问题
JSP传值到Servlet后端为NULL的问题 哔哩哔哩@萌狼蓝天 博客:萌狼工作室-博客园 联系QQ:#3447902411# | 仅限技术交流可添加 | 添加请说明你的方向和来意 1.目标文件路径 ...
- Spring MVC环境搭建和配置
1. 创建Dynamic web project 2. 修改WEB-INF/web.xml,内容如下: <?xml version="1.0" encoding=" ...
- [BUUCTF]REVERSE——[FlareOn4]IgniteMe
[FlareOn4]IgniteMe 附件 步骤: 例行检查,32位程序,无壳 32位ida载入 当满足第10行的if条件时,输出G00d j0b!提示我们成功,看一下sub_401050函数 3.s ...