506. Relative Ranks
Description
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
1st
place athlete's rank is"Gold Medal"
. - The
2nd
place athlete's rank is"Silver Medal"
. - The
3rd
place athlete's rank is"Bronze Medal"
. - For the
4th
place to thenth
place athlete, their rank is their placement number (i.e., thexth
place 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.length
1 <= n <= 104
0 <= score[i] <= 106
- All the values in
score
are unique.
Solutions
Solution 1: Sorting
We use an array $\textit{idx}$ to store the indices from $0$ to $n-1$, then sort $\textit{idx}$ based on the values in $\textit{score}$ in descending order.
Next, we define an array $\textit{top3} = [\text{Gold Medal}, \text{Silver Medal}, \text{Bronze Medal}]$. We traverse $\textit{idx}$, and for each index $j$, if $j$ is less than $3$, then $\textit{ans}[j]$ is $\textit{top3}[j]$; otherwise, it is $j+1$.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{score}$.
Python3
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
n = len(score)
idx = list(range(n))
idx.sort(key=lambda x: -score[x])
top3 = ["Gold Medal", "Silver Medal", "Bronze Medal"]
ans = [None] * n
for i, j in enumerate(idx):
ans[j] = top3[i] if i < 3 else str(i + 1)
return ans
Java
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i1, i2) -> score[i2] - score[i1]);
String[] ans = new String[n];
String[] top3 = new String[] {"Gold Medal", "Silver Medal", "Bronze Medal"};
for (int i = 0; i < n; ++i) {
ans[idx[i]] = i < 3 ? top3[i] : String.valueOf(i + 1);
}
return ans;
}
}
C++
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n = score.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&score](int a, int b) {
return score[a] > score[b];
});
vector<string> ans(n);
vector<string> top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
for (int i = 0; i < n; ++i) {
ans[idx[i]] = i < 3 ? top3[i] : to_string(i + 1);
}
return ans;
}
};
Go
func findRelativeRanks(score []int) []string {
n := len(score)
idx := make([][]int, n)
for i := 0; i < n; i++ {
idx[i] = []int{score[i], i}
}
sort.Slice(idx, func(i1, i2 int) bool {
return idx[i1][0] > idx[i2][0]
})
ans := make([]string, n)
top3 := []string{"Gold Medal", "Silver Medal", "Bronze Medal"}
for i := 0; i < n; i++ {
if i < 3 {
ans[idx[i][1]] = top3[i]
} else {
ans[idx[i][1]] = strconv.Itoa(i + 1)
}
}
return ans
}
TypeScript
function findRelativeRanks(score: number[]): string[] {
const n = score.length;
const idx = Array.from({ length: n }, (_, i) => i);
idx.sort((a, b) => score[b] - score[a]);
const top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal'];
const ans: string[] = Array(n);
for (let i = 0; i < n; i++) {
if (i < 3) {
ans[idx[i]] = top3[i];
} else {
ans[idx[i]] = (i + 1).toString();
}
}
return ans;
}