Skip to content

Commit 38ad0b7

Browse files
authored
Create rank-transform-of-an-array.cpp
1 parent b9c0ea9 commit 38ad0b7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/rank-transform-of-an-array.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
vector<int> arrayRankTransform(vector<int>& arr) {
7+
set<int> lookup{arr.cbegin(), arr.cend()};
8+
unordered_map<int, int> rank;
9+
for (const auto& v : lookup) {
10+
rank[v] = rank.size() + 1;
11+
}
12+
vector<int> result;
13+
transform(arr.cbegin(), arr.cend(), back_inserter(result),
14+
[&rank](const auto& v) {
15+
return rank[v];
16+
});
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)