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 120Sample Output 1
6
4
2
1Explanation 1
Alice starts playing with players already on the leaderboard, which looks like this:

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

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

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

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

Sample Input 2
6
100 90 90 80 75 60
5
50 65 77 90 102Sample Output 2
6
5
4
2
1Solution 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")
- The
climbingLeaderboardfunction takes two lists as arguments:rankedandplayer.rankedis a list of integers representing the scores of players already on the leaderboard.playeris a list of integers representing the scores of the player who wants to climb the leaderboard. - We create an empty list called
ranksto store the player's rank after each game. - We sort the
rankedlist in descending order and remove any duplicates using thesetfunction. This is done to eliminate any redundant values in therankedlist and to make it easier to compare values between therankedandplayerlists. - We initialize a variable
lto the length of therankedlist. This variable will be used to keep track of the player's rank as we loop through their scores. - We loop through each score in the
playerlist using aforloop. For each score, we enter awhileloop that checks if the score is greater than or equal to the score at indexl-1of therankedlist. If it is, we decrementlby 1 to move to the next score in therankedlist. We continue this until the score is less than the score at indexl-1orlreaches 0. This loop is used to find the player's rank for the current game. - Once we have found the player's rank for the current game, we add it to the
rankslist using theappendmethod. - After all games have been played, we return the
rankslist.
I hope this explanation helps! Let me know if you have any other questions