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.

    1. static public string[] FindRelativeRanks(int[] nums) {
    2. int[] ranks = new int[nums.Length];
    3. Array.Copy(nums, ranks, nums.Length);
    4. Array.Sort(ranks,(int a,int b)=> { return b - a; });
    5. Dictionary<int, int> dict = new Dictionary<int, int>();
    6. for (int i = 0; i < ranks.Length; i++) {
    7. dict[ranks[i]] = i + 1;
    8. }
    9. string[] resultArr = new string[nums.Length];
    10. for (int i = 0; i < nums.Length; i++) {
    11. int rank = dict[nums[i]];
    12. if (rank > 3) {
    13. resultArr[i] = rank.ToString();
    14. } else {
    15. switch (rank) {
    16. case 1:
    17. resultArr[i] = "Gold Medal";
    18. break;
    19. case 2:
    20. resultArr[i] = "Silver Medal";
    21. break;
    22. case 3:
    23. resultArr[i] = "Bronze Medal";
    24. break;
    25. }
    26. }
    27. }
    28. return resultArr;
    29. }





  • 相关阅读:
    ios上input的focus()、autofocus无效
    Object.assign()
    解决vue build后不兼容IOS11以下版本,并清理index缓存
    JavaScript常用方法
    mui-picker 增加过滤
    安装node-sass提示没有vendor目录的解决办法
    Base64编码
    object排序
    JSON数组去重
    vdom
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8c23f92d911573fa1b49d4640d87267c.html
Copyright © 2011-2022 走看看