Climbing the Leaderboard Hackerrank Solution |Codeityweb

Climbing the Leaderboard Hackerrank Solution 

In this post we are going to discuss the Climbing the Leaderboard Hackerrank Solution in java.

The problem statement is as follows:

An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this:
  • The player with the highest score is ranked number 1 on the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

 

Task :

To determine the rank of the player for a particular set of scores.

Understanding the problem statement:

  • You are given an array of scores that are scored by the players say array scores[100,90,90,80] and another array of scores for a particular player called as alice say alice_score[70,80,105].
  •  Now your task is to find the rank of alice for his score depending upon the the scores provided of all players.
  • Say in the above example the rank of the players will be 1,2,2,3 for 100,90,90,80 scores .
  • Now alice first score is 70 which is less then the last value i.e 80 so his rank will be 4.
  • For score=80 since it is same as last score so the rank will be 3.
  • And last score is 105 since it is the greatest score so the rank will be 1st. 

Climbing the Leaderboard Hackerrank Solution Approach:

 We will be using to steps in for this question.

  1. To Get the ranks of previous scores like the example given 1,2,2,3.
  2. To determine alice rank depending upon his scores.
     

Climbing the Leaderboard Hackerrank Solution Step 1 (Setting the ranks for the scores):

  • In this step we will use an array called rank to store the rank of the scores.
  • Rank of first score will be 1 followed by the consecutive ranks.
  • Say scores=[80,60,45,45,30],and rank array rank=[].
  • Now rank[0]=1,set the rank of score 80 as 1,now loop through the scores from 60 to 30 and give the ranks as per the scores.
  • if the score are same then the rank will be same as the previous one,rank[1]=2 for 60.
  • rank[2]=3 for 45,rank[3]=3 for 45 since score is same.
  • rank[4]=4 for 30 
In this way our rank array will be created
 
Step 1 (Setting the ranks for the scores) code in java:
 
int rank[]=new int[n];

rank[0]=1;
    for(int i=1;i<n;i++)
    {
        if(scores[i]==scores[i-1])
        {
            rank[i]=rank[i-1];
        }
        else
        {
            rank[i]=rank[i-1]+1;
        }
    


Climbing the Leaderboard Hackerrank Solution Step 2 (Determine rank for alice scores): 

  • Since their is array of scores for alice so we need to store the ranks in the array for alice scores as well for that we will create result array for alice that we are going to return to the calling function.
  • We run two loops the outer loop will traverse through the alice scores and inner loop will find its rank.
  • To find rank in the inner loop we traverse the scores from back to determine its appropriate position,once we found its appropriate position we give it the rank.

If you don't get it right now don't worry we will go through it by taking an sample test after the code.


Climbing the Leaderboard Hackerrank Solution Java:

static int[] climbingLeaderboard(int[] scores, int[] alice) {
    

     int n=scores.length;
    int m=alice.length;
    int rank[]=new int[n];

    rank[0]=1;
    for(int i=1;i<n;i++)
    {
        if(scores[i]==scores[i-1])
    
    {
    
        rank[i]=rank[i-1];
    
    }
        else
    
    {
    
        rank[i]=rank[i-1]+1;
    
    }
    }

    int a[]=new int[m];
    int index=n-1;

    for(int i=0;i<m;i++)
    {
        while(index>=0 && scores[index]<=alice[i])
    
    {
            index--;
    
    }
        if(index<0)a[i]=1;
        else
    
    {
    
        a[i]=rank[index]+1;
    
    }
    }
return a;
 

}

Output:
 
Climbing the Leaderboard Hackerrank Solution |Codeityweb


Climbing the Leaderboard Hackerrank Solution Working and Explanation:

  •  Lets take the same test case as in the above output and try to understand the working of the code , scores=[100,100,50,40,40,20,10] , alice_score=[5,25,50,120]
  • First we create rank array and set rank[0]=1 as per the code so the rank_array will be rank=[1]
  • Now we traverse through the scores array from i=1 i.e score 2 ,since we already gave rank as 1 for the first score.
  • Here we check if the score is same is previous score then we assing the same rank to it, here at i=1 score[1]=100 which is same as previous one so we give same rank to it so rank array will be rank=[1,1]
  • Now score[2]=50,since the score is not same is previous one so we increment the rank by one, so rank array will be rank=[1,1,2]
  • similarly other ranks will be given score[3]=40,rank =[1,1,2,3].
  • score[4]=40 rank=[1,1,2,3,3]
  • score[5]=20 ,rank=[1,1,2,3,3,4]
  • score[6]=10, rank=[1,1,2,3,3,4,5]
This was the first step we have successfully ranked the scores now we need to rank the alice scores.
  • We traverse through alice scores [5,25,50,120.
  • Now while the score is less than alice score we decrement the index.
Note :since the scores are also sorted we just need to assign the traversing index once to the last element,so we set index=n-1 .If the alice scores were not sorted we would assign the index=n-1 within the for loop instead to set the index to last element for proper position.
  • Once we found the proper position we assign the rank for that score same as the rank for previous scores.
That is it its a simple question , if you find it difficult to understand read the approach and go through the solution again you will definitely.

 
Other Posts:

Post a Comment

0 Comments