Skip to content

Commit baad7d4

Browse files
authored
Create relative-ranks.cpp
1 parent 08b691e commit baad7d4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

C++/relative-ranks.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
vector<string> findRelativeRanks(vector<int>& nums) {
7+
vector<int> indices(nums.size());
8+
iota(indices.begin(), indices.end(), 0);
9+
sort(indices.begin(), indices.end(),
10+
[&nums](int a, int b) {
11+
return nums[a] > nums[b];
12+
});
13+
14+
const vector<string> ranks = {"Gold Medal", "Silver Medal", "Bronze Medal"};
15+
vector<string> result(nums.size());
16+
for (int i = 0; i < nums.size(); ++i) {
17+
result[indices[i]] = i > 2 ? to_string(i + 1) : ranks[i];
18+
}
19+
return result;
20+
}
21+
};

0 commit comments

Comments
 (0)