Rank Transform of an Array | Leetcode 1331

preview_player
Показать описание
This video explains finding rank transform of an array using optimal set and hashmap approach.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
---------------------------------------------------------------------------------------------------------------------------------------------------------------

Рекомендации по теме
Комментарии
Автор

In case anyone islooking for the code of this explanation:
class Solution {
public:
vector<int> arr) {
int n=arr.size();
set<int>st;
for(auto it:arr){
st.insert(it);
}
map<int, int>mp; //{ele, rank}
vector<int>temp;
for(auto it:st){
temp.push_back(it);
}
for(int i=0; i<temp.size(); i++){
mp[temp[i]]=i+1;
}
vector<int>rank(n, 0);
for(int i=0; i<n; i++){
rank[i]=mp[arr[i]];
}
return rank;
}
};

rishabh._.raj
Автор

can we solve it without taking an extra vector ans as below: yeah still space complexity same O(n)
for(int i=0;i<n;i++){
arr[i]=mp[arr[i]];
}

vivekranjan
Автор

It would be lot easy if the order doesn't matter and has unique element only.. just sayin' :)

Thank you will back again tomorrow !!

sailendrachettri
Автор

sir plz start Leetcode challenge where we will solved daily 1 question leetcode

youknowsuraj.