You are viewing a single comment's thread. Return to all comments →
-> Remove duplicate elements from the array. -> Use binary search to get the new rank.
void removeDuplicates(vector<int> &arr){ int i; int temp=arr[0]; for(i=1;i<arr.size();i++){ if(arr[i]==temp){ arr[i]=-1; } else{ temp=arr[i]; } } vector<int>::iterator j; for(j=arr.begin();j!=arr.end();){ if(*j==-1){ arr.erase(j); } else{ j++; } } } int getRank(int value, vector<int> &arr) { cout<<arr.size()<<endl; if(value>=arr[0]){ return 1; } else if(value<arr[arr.size()-1]){ return arr.size()+1; } else if(value==arr[arr.size()-1]){ return arr.size(); } else{ int start=0; int end = arr.size()-1; int mid=0; while(start<=end){ mid = start + (end-start)/2; if(arr[mid]==value){ return mid+1; } else if(arr[mid]>value){ start=mid+1; } else{ end=mid-1; } } return start+1; } } vector<int> climbingLeaderboard(vector<int> ranked, vector<int> player) { vector<int> r; removeDuplicates(ranked); for(int i=0;i<player.size();i++){ r.push_back(getRank(player[i],ranked)); } return r; }
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →
SOLUTION IN C++
-> Remove duplicate elements from the array. -> Use binary search to get the new rank.