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:
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 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")
- The
climbingLeaderboard
function takes two lists as arguments:ranked
andplayer
.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. - We create an empty list called
ranks
to store the player's rank after each game. - We sort the
ranked
list in descending order and remove any duplicates using theset
function. This is done to eliminate any redundant values in theranked
list and to make it easier to compare values between theranked
andplayer
lists. - We initialize a variable
l
to the length of theranked
list. 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
player
list using afor
loop. For each score, we enter awhile
loop that checks if the score is greater than or equal to the score at indexl-1
of theranked
list. If it is, we decrementl
by 1 to move to the next score in theranked
list. We continue this until the score is less than the score at indexl-1
orl
reaches 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
ranks
list using theappend
method. - 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