506. Relative Ranks
2026/1/12大约 2 分钟约 739 字
506. Relative Ranks
难度: Easy
题目描述
You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.
The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
- The
1stplace athlete's rank is"Gold Medal". - The
2ndplace athlete's rank is"Silver Medal". - The
3rdplace athlete's rank is"Bronze Medal". - For the
4thplace to thenthplace athlete, their rank is their placement number (i.e., thexthplace athlete's rank is"x").
Return an array answer of size n where answer[i] is the rank of the ith athlete.
Example 1:
Input: score = [5,4,3,2,1] Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"] Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].
Example 2:
Input: score = [10,3,8,9,4] Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"] Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
Constraints:
n == score.length1 <= n <= 1040 <= score[i] <= 106- All the values in
scoreare unique.
解题思路
代码实现
解决方案
java
class Solution {
public String[] findRelativeRanks(int[] score) {
String[] temp = { "Gold Medal", "Silver Medal", "Bronze Medal" };
String[] res = new String[score.length];
int[] rank = sort(score);
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < rank.length; i++) {
map.put(rank[i], i + 1);
}
for (int i = 0; i < score.length; i++) {
if (map.get(score[i]) < 4) {
res[i] = temp[map.get(score[i]) - 1];
} else {
res[i] = map.get(score[i]) + "";
}
}
return res;
}
// 插入
private int[] sort(int[] data) {
if (data == null) {
throw new IllegalArgumentException("Input array cannot be null");
}
int[] sortedArray = Arrays.copyOf(data, data.length);
int currentVal = 0;
int position = 0;
for (int j = 1; j < sortedArray.length; j++) {
currentVal = sortedArray[j];
position = j - 1;
while (position >= 0 && sortedArray[position] < currentVal) {
sortedArray[position + 1] = sortedArray[position];
position--;
}
sortedArray[position + 1] = currentVal;
}
return sortedArray;
}
// 选择
// private int[] sort(int[] data) {
// int[] score = Arrays.copyOf(data, data.length);
// int maxIndex = -1;
// int maxValue = -1;
// for (int j = 0; j < score.length; j++) {
// maxIndex = j;
// maxValue=score[j];
// for (int i = j+1; i < score.length; i++) {
// if (score[i] > maxValue) {
// maxValue = score[i];
// maxIndex = i;
// }
// }
// swap(score,j,maxIndex);
// }
// return score;
// }
// 冒泡
// private int[] sort(int[] data) {
// int[] score = Arrays.copyOf(data, data.length);
// for (int i = 1; i < score.length; i++) {
// for (int j = 0; j < score.length - i; j++) {
// if (score[j] < score[j + 1]) {
// swap(score, j, j + 1);
// }
// }
// }
// return score;
// }
private void swap(int[] score, int i, int j) {
int temp = score[i];
score[i] = score[j];
score[j] = temp;
}
}