LC 1331. Rank Transform of an Array

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
public:
    vector<int> arrayRankTransform(vector<int>& arr) {
        unordered_map<int,int> umap;
        vector<int> store = arr;
        int n = arr.size(),j=1;
        if(n==0){
            return store;
        }
        sort(store.begin(),store.end());
        for(int i=0;i<n-1;i++){
            if(store[i]!=store[i+1]){
                umap[store[i]] = j++;
            }
        }
        umap[store[n-1]] = j++;
        for(int i=0;i<n;i++){
            store[i] = umap[arr[i]];
        }
        return store;
    }
};


Dicussion Forum