-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1331.cpp
More file actions
22 lines (21 loc) · 697 Bytes
/
1331.cpp
File metadata and controls
22 lines (21 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<pair<int, int>> ranking;
for (int i = 0; i < arr.size(); i++) ranking.push_back(make_pair(arr[i], i));
sort(ranking.begin(), ranking.end());
int rank = 1;
int index = 0;
while (index < ranking.size()) {
int curr = ranking[index].first;
arr[ranking[index].second] = rank;
while ((index + 1 < ranking.size()) && (ranking[index + 1].first == curr)) {
index++;
arr[ranking[index].second] = rank;
}
index++;
rank++;
}
return arr;
}
};