You are viewing a single comment's thread. Return to all comments →
My sweet ans simple solution in c++
vector<int> largestPermutation(int k, vector<int> arr) { int n=arr.size(); map<int,int>mp; for(int i=0;i<n;i++){ mp.insert({arr[i],i}); } vector<int> a(arr); sort(a.begin(),a.end()); int swp=0; for(int i=0;i<n-1;i++){ int max_e=a[a.size()-1]; int max_i=mp[max_e]; a.pop_back(); if(max_i==i) continue; else{ swp++; swap(arr[i],arr[max_i]); mp[arr[i]]=i; mp[arr[max_i]]=max_i; } if(swp==k)break; } return arr; }
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Permutation
You are viewing a single comment's thread. Return to all comments →
My sweet ans simple solution in c++