class Solution:
def findRelativeRanks(self, score):
# Pair each score with its index
indexed = list(enumerate(score))
# Sort by score in descending order
indexed.sort(key=lambda x: -x[1])
result = [""] * len(score)
for rank, (idx, sc) in enumerate(indexed):
if rank == 0:
result[idx] = "Gold Medal"
elif rank == 1:
result[idx] = "Silver Medal"
elif rank == 2:
result[idx] = "Bronze Medal"
else:
result[idx] = str(rank + 1)
return result
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n = score.size();
vector<pair<int, int>> indexed;
for (int i = 0; i < n; ++i) {
indexed.push_back({score[i], i});
}
sort(indexed.rbegin(), indexed.rend());
vector<string> result(n);
for (int rank = 0; rank < n; ++rank) {
int idx = indexed[rank].second;
if (rank == 0)
result[idx] = "Gold Medal";
else if (rank == 1)
result[idx] = "Silver Medal";
else if (rank == 2)
result[idx] = "Bronze Medal";
else
result[idx] = to_string(rank + 1);
}
return result;
}
};
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0] = score[i];
indexed[i][1] = i;
}
Arrays.sort(indexed, (a, b) -> b[0] - a[0]);
String[] result = new String[n];
for (int rank = 0; rank < n; rank++) {
int idx = indexed[rank][1];
if (rank == 0)
result[idx] = "Gold Medal";
else if (rank == 1)
result[idx] = "Silver Medal";
else if (rank == 2)
result[idx] = "Bronze Medal";
else
result[idx] = String.valueOf(rank + 1);
}
return result;
}
}
var findRelativeRanks = function(score) {
const indexed = score.map((sc, idx) => [sc, idx]);
indexed.sort((a, b) => b[0] - a[0]);
const result = Array(score.length);
for (let rank = 0; rank < indexed.length; rank++) {
const idx = indexed[rank][1];
if (rank === 0)
result[idx] = "Gold Medal";
else if (rank === 1)
result[idx] = "Silver Medal";
else if (rank === 2)
result[idx] = "Bronze Medal";
else
result[idx] = String(rank + 1);
}
return result;
};
You are given an array score
of size n
, where each element represents the score of an athlete in a competition. Your task is to assign a relative rank to each athlete based on their score:
"Gold Medal"
."Silver Medal"
."Bronze Medal"
.
The result should be an array of strings result
where result[i]
corresponds to the rank of the athlete at score[i]
in the original array. Each athlete should have exactly one rank, and no two athletes can share the same rank.
To solve this problem, we need to determine the relative standing of each athlete based on their score. A brute-force approach might involve repeatedly finding the maximum score and assigning ranks one by one, but this would be inefficient, especially for large arrays.
Instead, we realize that sorting the scores in descending order gives us the ranking order directly. By keeping track of the original indices, we can assign the correct rank to each athlete in the original order. This approach is much more efficient than checking each score individually for its rank.
The key insight is to pair each score with its index, sort these pairs by score, and then assign the appropriate rank string based on each athlete's position in the sorted list.
Let's break down the steps to solve the problem:
We use a list or array for the result so that we can efficiently update each athlete's rank by their original index. Sorting is used because it gives us the ranking in O(n log n) time, which is efficient for this problem.
Let's consider the input score = [5, 4, 3, 2, 1]
.
[(5,0), (4,1), (3,2), (2,3), (1,4)]
[(5,0), (4,1), (3,2), (2,3), (1,4)]
(already sorted)
["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Notice how each athlete's rank is placed at their original index, preserving the input order.
Brute-force Approach:
The optimized approach is efficient and suitable for large input sizes.
To assign relative ranks to athletes based on their scores, we pair each score with its index, sort these pairs in descending order, and then assign rank strings based on their sorted positions. This method is efficient, easy to implement, and leverages sorting to avoid unnecessary repeated comparisons. The solution is both elegant and practical, making use of data structures and sorting to achieve the desired result efficiently.