Skip to content

Commit 9a2b735

Browse files
authored
Create 1331. Rank Transform of an Array (#601)
2 parents 5b7450b + 9af80d7 commit 9a2b735

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

1331. Rank Transform of an Array

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Approach - 01 [ No Sorting function]
2+
class Solution {
3+
public:
4+
vector<int> arrayRankTransform(vector<int>& a) {
5+
map<int,int> mp;
6+
// store values in ordered map
7+
for(auto& val: a){
8+
mp[val]++;
9+
}
10+
11+
// start assign value their rank
12+
// from top to bottom
13+
int rank=1;
14+
for(auto& val:mp){
15+
val.second = rank;
16+
rank++;
17+
}
18+
19+
// traverse on array and assign them
20+
// rank based on map
21+
vector<int> ans(a.size());
22+
for(int i=0;i<a.size();i++){
23+
ans[i] = mp[a[i]];
24+
}
25+
26+
// return the ranks
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)