给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”(”Gold Medal”, “Silver Medal”, “Bronze Medal”)
(注:分数越高的选手,排名越靠前。)
示例 1:
输入: [5, 4, 3, 2, 1] 输出: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”] 解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” (“Gold Medal”, “Silver Medal” and “Bronze Medal”). 余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
- N 是一个正整数并且不会超过 10000。
- 所有运动员的成绩都不相同。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/relative-ranks
class Solution {public String[] findRelativeRanks(int[] score) {String[] result = new String[score.length];// 定义一个change 用于将每次找到的成绩最大值都换成改值避免重复int change = Integer.MIN_VALUE;for (int i = 0; i < score.length; i++) {int maxScoreIndex = findMaxScoreIndex(score);score[maxScoreIndex] = change;if (i == 0){result[maxScoreIndex] = "Gold Medal";}else if (i == 1){result[maxScoreIndex] = "Silver Medal";}else if (i == 2){result[maxScoreIndex] = "Bronze Medal";}else {result[maxScoreIndex] = String.valueOf(i + 1);}}return result;}// 查找成绩最大值的下标public int findMaxScoreIndex(int[] score){int max = Integer.MIN_VALUE;int index = 0;for (int i = 0; i < score.length; i++) {if (score[i] > max){max = score[i];index = i;}}return index;}}
