zoukankan      html  css  js  c++  java
  • leetcode 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: ["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.

    关联值  和 下标 然后排序,最后在对应到原来的序列

    class Solution {
    public:
        vector<string> findRelativeRanks(vector<int>& nums) {
            vector<pair<int, int> > v;
            for (int i = 0; i < nums.size(); ++i) {
                pair<int, int> p;
                p.first = nums[i];
                p.second = i;
                v.push_back(p);
            }
            sort(v.begin(), v.end());
            vector<string> s;
            map<int, int> mp;
            for (int i = v.size() - 1; i >= 0; --i) {
                mp[v[i].second] = v.size() - i;
            }
            for (int i = 0; i < nums.size(); ++i) {
                int x = mp[i];
                if (x == 1) s.push_back("Gold Medal");
                else  if (x == 2) s.push_back("Silver Medal");
                else if (x == 3) s.push_back("Bronze Medal");
                else s.push_back(to_string(x));
            }
            return s;
        }
    };
  • 相关阅读:
    AGC 015 E
    CF 1041 F. Ray in the tube
    AGC 005 D
    CF 348 D. Turtles
    2069: [POI2004]ZAW
    AGC 007 D
    zhengruioi 470 区间
    2653: middle
    Django 源码安装及使用
    Django MTV模型思想
  • 原文地址:https://www.cnblogs.com/pk28/p/7305525.html
Copyright © 2011-2022 走看看