[leetcode]: 506. Relative Ranks
1.题目描述
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”.
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”.
翻译:给出一个长度为N的运动员得分数组,求每个运动员对应的名次。
其中1,2,3的表达为”Gold Medal”, “Silver Medal”, “Bronze Medal”
2.分析
这个题目考察的是排序并且需要知道排序后的元素原本的下标。
大致看到三种解法:
(1)先保存各个得分的下标。用字典。因为得分都是唯一的。再排序。
(2)自定义比较函数,根据得分大小排序,但参与排序的是下标序列。
(3)使用数据结构,priority_queue
我用python写的,采用的是第一种解法。先用字典保存元素下标再排序。
3.代码
python
def findRelativeRanks(nums):#记录每个分数值对应排名pos={n:i+1 for i,n in enumerate(sorted(nums,reverse=True))}def f(x):if pos[x]==1:return "Gold Medal"elif pos[x]==2:return "Silver Medal"elif pos[x]==3:return "Bronze Medal"else:return str(pos[x])return map(f,nums)
也有人把代码缩减到很短
def findRelativeRanks(self, nums):sort = sorted(nums)[::-1]rank = ["Gold Medal", "Silver Medal", "Bronze Medal"] + map(str, range(4, len(nums) + 1))return map(dict(zip(sort, rank)).get, nums)
自定义排序函数的c++解法我觉得是很赞的,这里把那位大牛的解法贴出来学习一下。
class Solution {
public:vector<string> findRelativeRanks(vector<int>& nums) {vector<int> rank;for(int i=0; i<nums.size(); ++i) rank.push_back(i);sort(rank.begin(), rank.end(), [&](int a, int b){return nums[a] > nums[b];});vector<string> ranks(nums.size());for(int i=3; i<nums.size(); ++i){ranks[rank[i]] = std::to_string(i+1);}if(nums.size() > 0) ranks[rank[0]] = "Gold Medal";if(nums.size() > 1) ranks[rank[1]] = "Silver Medal";if(nums.size() > 2) ranks[rank[2]] = "Bronze Medal";return ranks;}
};
[leetcode]: 506. Relative Ranks相关推荐
- [LeetCode]506. Relative Ranks
[LeetCode]506. Relative Ranks 题目描述 思路 用map保存 计算res的时候直接逆序遍历map即可 代码 #include <iostream> #inclu ...
- LeetCode | 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode之Relative Ranks
1.题目 Given scores of N athletes, find their relative ranks and the people with the top three highest ...
- LeetCode Relative Ranks
今天做了大概四五道题,但觉着这道题是最值得分享的吧,Relative Ranks意思是相对排名问题,下面是题目具体内容,大家可以通过翻译试着做一下: Given scores of N athlete ...
- LeetCode 506. 相对名次(map)
1. 题目 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌. 前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌" ...
- leetcode 506. 相对名次(Java版)
题目 https://leetcode-cn.com/problems/relative-ranks/ 题解 思路主要是,将运动员一开始的位置记录下来,方便颁奖的时候去指定位置颁给指定的人. 先用ma ...
- leetcode 506 相对名次
https://leetcode-cn.com/problems/relative-ranks/ 题目 给你一个长度为nnn的整数数组scorescorescore,其中score[i]score[i ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
- Leetcode算法题-解法转载
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
最新文章
- C#发现之旅第九讲 ASP.NET验证码技术
- 怎样使用element-starter快速搭建ElementUI项目
- 24.内存操作Copy-Move-Clone.rs
- 怎么看电脑系统是win几_一文看懂arm架构和x86架构有什么区别
- 各平台安装Node.js
- 什么样的 python 可以可谓专业 PyPI 项目?刚刚学到三个概念:pep8、Sphinx、pytest与GitHub Action的集成
- 数据库设计三范式(3NF)
- IIS发布web网站
- 深度思考:广州互联网气氛为何远远落后于北京
- 同治光绪之交的九江城
- iOS中更新版权 Copyright
- 闭关修炼(二十五)基础web安全
- c语言位段实现字节异或,C语言-位运算-小结
- 《Counting Out Time: Class Agnostic Video Repetition Counting in the Wild》论文笔记
- bsd mac linux,macos-Mac OS X和BSD有多紧密的联系?
- oracle 行列对应,Oracle行列互换 横表和纵表
- oracle to_char ww,oracle中Select To_Char(Sysdate, 'ww') From Dual;这是什么意思
- 【优化求解】基于matlab求解能谱效率优化问题【含Matlab源码 1836期】
- 简述软件全面质量管理的思想体系
- Excel如何在混合文本中提取数字