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





原文地址:https://www.cnblogs.com/xiejunzhao/p/8c23f92d911573fa1b49d4640d87267c.html