Skip to content

Create 1331. Rank Transform of an Array #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 1331. Rank Transform of an Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Approach - 01 [ No Sorting function]
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& a) {
map<int,int> mp;
// store values in ordered map
for(auto& val: a){
mp[val]++;
}

// start assign value their rank
// from top to bottom
int rank=1;
for(auto& val:mp){
val.second = rank;
rank++;
}

// traverse on array and assign them
// rank based on map
vector<int> ans(a.size());
for(int i=0;i<a.size();i++){
ans[i] = mp[a[i]];
}

// return the ranks
return ans;
}
};
Loading