【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 ...
随机推荐
- 网络爬虫-python-爬取天涯求职贴
使用urllib请求页面,使用BeautifulSoup解析页面,使用xlwt3写入Excel import urllib.request from bs4 import BeautifulSoup ...
- .net与java建立WebService再互相调用
A: .net建立WebService,在java中调用. 1.在vs中新建web 简单修改一下Service.cs的[WebMethod]代码: using System; using System ...
- [C++] vptr, where are you?
Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...
- A Child's History of England.25
It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...
- linux下的C++多线程
原文链接:http://blog.csdn.net/lee1054908698/article/details/54633056 本随笔作为多线程笔记使用,内容完全照搬原博 多线程是多任务处理的一种特 ...
- 【Netty】最透彻的Netty原理架构解析
这可能是目前最透彻的Netty原理架构解析 本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. ...
- HDFS初探之旅(二)
6.HDFS API详解 Hadoop中关于文件操作类疾病上全部在"org.apache.hadoop.fs"包中,这些API能够支持的操作包含:打开文件.读写文件.删除文件等. ...
- 格式化代码(Eclipse 格式化代码块快捷键:Ctrl+Shift+F)
1.格式化java代码 : ①Ctrl+Shift+F 但是我们会遇到按 Ctrl+Shift+F不起作用的时候? Ctrl+Shift+F 在搜狗拼音里是简繁替换.一旦安装搜狗拼音这个快 ...
- java设计模式—Decorator装饰者模式
一.装饰者模式 1.定义及作用 该模式以对客户端透明的方式扩展对象的功能. 2.涉及角色 抽象构件角色:定义一个抽象接口,来规范准备附加功能的类. 具体构件角色:将要被附加功能的类,实现抽象 ...
- Mave 下载与安装
一,Maven 介绍 我们在开发中经常需要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还需要将旧的包升级或降级,当项目复杂到一定程度时包管理变得非常重要.Maven是当前最受欢迎 ...