[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: ["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.

Note:

  1. N is a positive integer and won‘t exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

使用一个临时数组存储原数组按逆序排序后的元素,遍历临时数组,找出临时数组中元素在原数组中的位置。然后根据该位置在结果字符串数组中添加排名信息。效率不是很高,但是还是成功AC。

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        vector<string> res(nums.size(), "");
        vector<int> tmp(nums.begin(), nums.end());
        sort(tmp.begin(), tmp.end(), [](int a, int b) { return a > b; });
        for (int i = 0; i != tmp.size(); i++) {
            for (int j = 0; j != nums.size(); j++) {
                if (nums[j] == tmp[i]) {
                    if (i == 0)
                        res[j] = "Gold Medal";
                    else if (i == 1)
                        res[j] = "Silver Medal";
                    else if (i == 2)
                        res[j] = "Bronze Medal";
                    else
                        res[j] = to_string(i + 1);
                }
            }
        }
        return res;
    }
};
// 209 ms
时间: 2024-11-04 05:25:28

[LeetCode] Relative Ranks的相关文章

[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】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算法题-Relative Ranks(Java实现)

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

leetcode 506. 相对名次(Relative Ranks)

目录 题目描述: 示例 1: 解法: 题目描述: 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌"("Gold Medal", "Silver Medal", "Bronze Medal"). (注:分数越高的选手,排名越靠前.) 示例 1: 输入: [5, 4, 3, 2, 1] 输出: ["Go

[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】未分类(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