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

    C++(13ms): 或者用优先队列

     1 class Solution {
     2 public:
     3     vector<string> findRelativeRanks(vector<int>& nums) {
     4          vector<pair<int,int> > vec;
     5          for (int i=0; i < nums.size(); i++){
     6             vec.push_back(make_pair(nums[i],i));
     7          }
     8          sort(vec.rbegin(),vec.rend());
     9          vector<string> res(nums.size(),"");
    10          int count=1;
    11          for (int i=0; i < nums.size(); i++){
    12             if (count==1){
    13                 res[vec[i].second] = "Gold Medal";
    14                 count++;
    15             }else if(count == 2){
    16                 res[vec[i].second] = "Silver Medal";
    17                 count++;
    18             }
    19             else if(count == 3){
    20                 res[vec[i].second] = "Bronze Medal";
    21                 count++;
    22             }else{
    23                 res[vec[i].second] = to_string(count);
    24                 count++;
    25             }
    26     
    27          }
    28      return res;
    29     }
    30 };
  • 相关阅读:
    看完一本,加油
    一个简单的动作,让你的手机号码变成空号
    Goldwave心得
    UML设计初步 基本概念一(actor, use case)
    ASP.NET控件开发 概念和HelloWorld控件
    控件的呈现
    ASP.NET控件生命周期
    ASP老项目中如何搜索一个文件在哪些地方被引用
    PL/SQL语法 游标
    2009编程语言排名
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/6479335.html
Copyright © 2011-2022 走看看