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





  • 相关阅读:
    推荐系统中的Graph Model
    Stanford机器学习课程之一——引言
    Sigmoid函数
    高斯RBF核函数中Sigma取值和SVM分离面的影响
    交叉验证(cross validation)
    匿名函数lambda和map函数
    ActionsChains类鼠标事件和Keys类键盘事件
    类、继承和反射
    WebDriverWait类以及类常用的方法
    frame的处理
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/8c23f92d911573fa1b49d4640d87267c.html
Copyright © 2011-2022 走看看