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. }





  • 相关阅读:
    完整的WSDL语法
    WSDL UDDI
    八一八 The Social Network的小细节
    MySQL命令行常用命令
    AspectJ风格的Aop切点表达式
    强大的Mockito测试框架
    MySQL锁定状态查看命令
    Yum本地Rpm库设置
    Sed实例大全
    为何 Emacs 和 Vim 被称为两大神器
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8c23f92d911573fa1b49d4640d87267c.html
Copyright © 2011-2022 走看看