Hackerrank - Climbing the Leaderboard Solution

Hackerrank - Climbing the Leaderboard Solution

Alice is playing an arcade game and wants to climb to the top of the leaderboard and wants to track her ranking. The game uses Dense Ranking, so its leaderboard works like this:

  • The player with the highest score is ranked number  on the leaderboard.
  • Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

For example, the four players on the leaderboard have high scores of , , , and . Those players will have ranks , , , and , respectively. If Alice's scores are ,  and , her rankings after each game are ,  and .

Function Description

Complete the climbingLeaderboard function in the editor below. It should return an integer array where each element  represents Alice's rank after the  game.

climbingLeaderboard has the following parameter(s):

  • scores: an array of integers that represent leaderboard scores
  • alice: an array of integers that represent Alice's scores

Input Format

The first line contains an integer , the number of players on the leaderboard.
The next line contains  space-separated integers , the leaderboard scores in decreasing order.
The next line contains an integer, , denoting the number games Alice plays.
The last line contains  space-separated integers , the game scores.

Constraints

  • for
  • for
  • The existing leaderboard, , is in descending order.
  • Alice's scores, , are in ascending order.

Subtask

For  of the maximum score:

Output Format

Print  integers. The  integer should indicate Alice's rank after playing the  game.

Sample Input 1

7
100 100 50 40 40 20 10
4
5 25 50 120

Sample Output 1

6
4
2
1

Explanation 1

Alice starts playing with  players already on the leaderboard, which looks like this:

image

After Alice finishes game , her score is  and her ranking is :

image

After Alice finishes game , her score is  and her ranking is :

image

After Alice finishes game , her score is  and her ranking is tied with Caroline at :

image

After Alice finishes game , her score is  and her ranking is :

image

Sample Input 2

6
100 90 90 80 75 60
5
50 65 77 90 102

Sample Output 2

6
5
4
2
1

Solution in Python

def climbingLeaderboard(ranked, player):
    ranks = [] # Create an empty list to store player's ranks
    ranked = sorted(set(ranked), reverse=True) # Sort the ranked list in descending order and remove duplicates
    l = len(ranked) # Initialize a variable l to the length of the ranked list
    for score in player: # Loop through each score in the player list
        while (l > 0) and (score >= ranked[l-1]): # Check if the score is greater than or equal to the current score in ranked list
            l -= 1 # If it is, decrement l to move to the next score in ranked list
        ranks.append(l+1) # Add the player's rank to the ranks list
    return ranks # Return the list of ranks

input()
scores = list(map(int,input().split()))
input()
alice = list(map(int,input().split()))

print(*climbingLeaderboard(scores,alice),sep="\n")
  1. The climbingLeaderboard function takes two lists as arguments: ranked and player. ranked is a list of integers representing the scores of players already on the leaderboard. player is a list of integers representing the scores of the player who wants to climb the leaderboard.
  2. We create an empty list called ranks to store the player's rank after each game.
  3. We sort the ranked list in descending order and remove any duplicates using the set function. This is done to eliminate any redundant values in the ranked list and to make it easier to compare values between the ranked and player lists.
  4. We initialize a variable l to the length of the ranked list. This variable will be used to keep track of the player's rank as we loop through their scores.
  5. We loop through each score in the player list using a for loop. For each score, we enter a while loop that checks if the score is greater than or equal to the score at index l-1 of the ranked list. If it is, we decrement l by 1 to move to the next score in the ranked list. We continue this until the score is less than the score at index l-1 or l reaches 0. This loop is used to find the player's rank for the current game.
  6. Once we have found the player's rank for the current game, we add it to the ranks list using the append method.
  7. After all games have been played, we return the ranks list.

I hope this explanation helps! Let me know if you have any other questions

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe