-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1331.cpp
More file actions
37 lines (34 loc) · 970 Bytes
/
1331.cpp
File metadata and controls
37 lines (34 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
if (arr.empty()) return {};
vector<pair<int, int>> ais;
for (int i = 0; i < arr.size(); ++i)
ais.emplace_back(arr[i], i);
sort(ais.begin(), ais.end());
vector<int> ans(arr.size());
int rank = 1, pre = ais[0].first;
for (auto ai : ais) {
if (ai.first == pre)
ans[ai.second] = rank;
else {
ans[ai.second] = ++rank;
pre = ai.first;
}
}
return ans;
}
};
/*
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
vector<int> ans = arr;
sort(arr.begin(), arr.end());
int len = unique(arr.begin(), arr.end()) - arr.begin();
for(int& a : ans)
a = lower_bound(arr.begin(), arr.begin() + len, a) - arr.begin() + 1;
return ans;
}
};
*/