zoukankan      html  css  js  c++  java
  • LeetCode

    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 {
        class Athlete {
            int index;
            int score;
            public Athlete(int index, int score) {
                this.index = index;
                this.score = score;
            }
        }
        public String[] findRelativeRanks(int[] nums) {
            if (nums == null || nums.length <= 0)
                return new String[0];
            String[] ret = new String[nums.length];
            Athlete[] athletes = new Athlete[nums.length];
            for (int i=0; i<nums.length; i++) {
                athletes[i] = new Athlete(i, nums[i]);
            }
            Arrays.sort(athletes, new Comparator<Athlete>() {
                @Override
                public int compare(Athlete o1, Athlete o2) {
                    return o2.score - o1.score;
                }
            });
            for (int i=0; i<athletes.length; i++) {
                Athlete a = athletes[i];
                if (i == 0)
                    ret[a.index] = "Gold Medal";
                else if (i == 1)
                    ret[a.index] = "Silver Medal";
                else if (i == 2)
                    ret[a.index] = "Bronze Medal";
                else 
                    ret[a.index] = String.valueOf(i+1);
            }
            return ret;
        }
    }
     
  • 相关阅读:
    IBM斥资340亿美元收购红帽
    单例模式讨论篇:单例模式与垃圾回收
    Xshell拖拽上传文件插件
    理想的程序员
    Android学习之路
    springboot更改启动logo,佛祖保佑 ,永不宕机 , 永无BUG
    Java多线程实现的四种方式
    IntelliJ IDEA
    Jrebel最新激活破解方式(持续更新)
    PyCharm 2018.2.4永久破解办法
  • 原文地址:https://www.cnblogs.com/wxisme/p/9487350.html
Copyright © 2011-2022 走看看