zoukankan      html  css  js  c++  java
  • 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.
     1 public class Solution {
     2     public String[] findRelativeRanks(int[] nums) {
     3         int n = nums.length;
     4         
     5         HashMap map = new HashMap();
     6         for (int i = 0; i < n; i++) {
     7             map.put(nums[i], i);
     8         }
     9         
    10         Arrays.sort(nums);
    11         String[] result = new String[n];
    12         
    13         for (int i = 0; i < n; i++) {
    14             int index = (int) map.get(nums[i]);
    15             
    16             if (i == n - 1) result[index] = "Gold Medal";
    17             else if (i == n - 2) result[index] = "Silver Medal";
    18             else if (i == n - 3) result[index] = "Bronze Medal";
    19             else result[index] = Integer.toString(n - i);
    20         }
    21         return result;
    22     }
    23 }
  • 相关阅读:
    WSL2
    坐标系变换
    Python websocket
    PAJ7620 IIC 通信
    Python中assert的使用
    Python中循环的使用
    Linux 生成指定大小文件
    SVN不显示log 显示1970年问题
    阿里云 CS实例 开机自运行脚本文件
    生成UDS安全算法DLL文件
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6391364.html
Copyright © 2011-2022 走看看