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;
        }
    };
  • 相关阅读:
    委托系列整理
    EF Lambda 多表查询
    枚举,Enum,常规使用demo记录
    自定义Window 服务
    xpath 操作XML
    MVC 自定义过滤器
    时间比对,常用细节记录
    Lock锁_线程_线程域
    break、continue和goto 三者作用介绍
    .net 学习路线感想
  • 原文地址:https://www.cnblogs.com/pk28/p/7305525.html
Copyright © 2011-2022 走看看