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;
        }
    };
  • 相关阅读:
    正则表达式简介
    SQL 语言类型
    C# 与 SQL Server 的数据类型对应关系
    Visual Studio 各版本下载
    使用 Parallel LINQ 进行数据分页
    操作系统版本
    C# 命名规范
    解决chrome浏览器在win8下没有注册类的问题
    HttpClient的基本使用
    HttpClient介绍
  • 原文地址:https://www.cnblogs.com/pk28/p/7305525.html
Copyright © 2011-2022 走看看