leetcode 506. 相对名次(Relative Ranks)

目录

  • 题目描述:
  • 示例 1:
  • 解法:

题目描述:

给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。

(注:分数越高的选手,排名越靠前。)

示例 1:

输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。

提示:

  1. N 是一个正整数并且不会超过 10000。
  2. 所有运动员的成绩都不相同。

解法:

# define PR pair<int, int>

class Solution {
public:
    string toString(int num){
        if(num == 1){
            return "Gold Medal";
        }else if(num == 2){
            return "Silver Medal";
        }else if(num == 3){
            return "Bronze Medal";
        }else{
            string res = "";
            while(num != 0){
                res = char(num%10 + '0') + res;
                num /= 10;
            }
            return res;
        }
    }

    vector<string> findRelativeRanks(vector<int>& nums) {
        int sz = nums.size();
        vector<PR> lst;
        for(int i = 0; i < sz; i++){
            lst.push_back(make_pair(-nums[i], i));
        }
        sort(lst.begin(), lst.end());
        vector<string> res(sz, "");
        for(int i = 0; i < sz; i++){
            // cout<<lst[i].first<<", "<<lst[i].second<<endl;
            res[lst[i].second] = toString(i+1);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10595021.html

时间: 2024-08-10 01:20:14

leetcode 506. 相对名次(Relative Ranks)的相关文章

【leetcode】506. Relative Ranks

problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: vector<string> findRelativeRanks(vector<int>& nums) { priority_queue<pair<int, int>> myqueue;// for(int i=0; i<nums.size(); i

[LeetCode&amp;Python] Problem 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: [&

[LeetCode] 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: [&

[LeetCode] 506. Relative Ranks_Easy tag: Sort

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: [&

LeetCode算法题-Relative Ranks(Java实现)

这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个人,他们将获得奖牌:"金牌","银牌"和"铜牌".例如: 输入:[5,4,3,2,1] 输出:["Gold Medal","Silver Medal","Bronze Medal",&quo

【LeetCode】未分类(tag里面没有)(共题)

[419]Battleships in a Board (2018年11月25日)(谷歌的题,没分类.) 给了一个二维平面,上面有 X 和 . 两种字符. 一行或者一列连续的 X 代表一个战舰,问图中有多少个战舰.(题目要求one pass, 空间复杂度是常数) 题目说了每两个战舰之间起码有一个 . 作为分隔符,所以不存在正好交叉的情况. 题解:我提交了一个 floodfill 的题解,能过但是显然不满足要求. discuss里面说,我们可以统计战舰的最上方和最左方,从而来统计战舰的个数.(这个

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

leetcode-24-exercise

506. Relative Ranks 解题思路: 使用priority_queue.它在插入时会将数据按照由大到小的顺序插入,自然排序了.所以插入时考虑插入pair<nums[i],i>,然后根据i填充result. 注意,1) priority_queue没有迭代器,所以要遍历只能通过pop操作 2) vector要注意初始化,不然访问时可能有问题 #include <queue> class Solution { public: vector<string> fi