Improved Ranking Leaderboard Problem Algo.
vector<int> rankGiver(vector<int> arr){
int temp;
long arr_size=arr.size();
vector<int> array(arr);
long array_size=array.size();
for(long i=0;i<arr_size-1;i++){
for(long j=0;j<arr_size-1-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
// cout<<"\nThe array passed to the function rankGiver after sorting
process\n";
// display(arr);
vector<int> rank(arr_size);
rank[arr.size()-1]=1;
for(long i=arr_size-2;i>=0;i--){
if(arr[i]==arr[i+1]){
rank[i]=rank[i+1];
}else{
rank[i]=rank[i+1]+1;
}
}
cout<<endl;
// cout<<"The rank array inside the function rankGiver\n";
// display(rank);
for(long k=0;k<array_size;k++){
/*In the below for loop I have made a change
*Instead of l=k+1, I have done l=k,because
*bcz it is possible that the first element of
*original "array" maybe equal to the first
*element of the "arr" array and then algo
*won't find the number
*/
for(long l=0;l<array_size;l++){
if(array[k]==arr[l]){
array[k]=rank[l];
}
}
}
// cout<<"\nThe final returned array by the function\n";
return array;
}
vector<int> climbingLeaderboard(vector<int> ranked, vector<int> player) {
long player_size=player.size();
vector<int> ans;
vector<int> arr(ranked);
vector<int> real_ans;
for(long j=0;j<player_size;j++){
if(j>0){
arr.pop_back();
}
arr.push_back(player[j]);
// for(int& y:arr){
// cout<<y<<" ";
// }cout<<endl;
ans=rankGiver(arr);
// for(int& z:real_ans){
// cout<<z<<" ";
// }cout<<"\n";
real_ans.push_back(ans[ans.size()-1]);
}
// cout<<"Printing ans array \n";
// for(int& a:ans){
// cout<<a<<" ";
// }cout<<"\n";
// for(int& x:real_ans){
// cout<<x<<" ";
// }cout<<"\n";
return real_ans;
}
Comments
Post a Comment