Climbing the Leaderboard Algorithm

Photo by Sigmund on Unsplash

Climbing the Leaderboard Algorithm

"Chaos is a leaderboard..."

ยท

5 min read

Howdy fellow programmers! ๐Ÿ‘‹๐Ÿฝ

In this article, I'll be describing how I solved the Climbing the Leaderboard Challenge on HackerRank using Python 3.

This challenge is part of the Implementation challenges in the Algorithm section on HackerRank.

If this is your first time reading my data structures and algorithms article, please consider reading more in my Algos in Plain English series.๐Ÿ˜ƒ

Problem ๐ŸŽฒ

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.

Example

ranked = [100, 90, 90, 80]
player = [70, 80, 105]

The ranked players will have ranks 1, 2, 2, and 3, respectively. If the player's scores are 70, 80 and 105, their rankings after each game are 4th, 3rd and 1st. Return [4, 3, 1].

Function Description

Complete the climbingLeaderboard function in the editor below.

climbingLeaderboard has the following parameter(s):

  • int ranked[n]: the leaderboard scores
  • int player[m]: the player's scores

Returns

  • int[m]: the player's rank after each new score

Input Format

The first line contains an integer n, the number of players on the leaderboard.

The next line contains n space-separated integers ranked[i], the leaderboard scores in decreasing order.

The next line contains an integer, m, the number of games the player plays.

The last line contains m space-separated integers `player[j], the game scores.

Sample Input 0:

image.png

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

Sample Output 0:

6
4
2
1

Explanation:

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

image.png

After Alice finishes game 0, her score is 5 and her ranking is 6:

image.png

After Alice finishes game 1, her score is 25 and her ranking is 4:

image.png

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

image.png

After Alice finishes game 3, her score is 120 and her ranking is 1:

image.png

I'll get right into the algorithm. Please feel free to read the full question on HackerRank.

Solution ๐Ÿ”Ž

In plain English, we need to determine a player's rank in an existing leaderboard after every game.

MasseyFergusonGIF.gif To test our solution, let's consider the same sample input from above.

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

Let's write down the steps we'll use to solve this problem.

# Sort the 'ranked' array so we can get a structured leaderboard.
# Get the length of the leaderboard to track where we are at each iteration.
# Loop through the 'player' array and compare against our leaderboard logic.
# Go up the leaderboard (ranked) until a score in 'player' is greater than or equal to the ranked score
# At this point, we'll break out of the loop, add 1 to the ranking and go to the next score in 'player'

Step 1๏ธโƒฃ: Sort the ranked array to emulate a leaderboard.

From the problem constraints, we are not certain the input array will be sorted, so we need to sort ranked to emulate a real-world leaderboard. We'll store this sorted array in leaderboard

# Sort the 'ranked' array so we can get a structured leaderboard.
def climbingLeaderboard(ranked, player):
    leaderboard = sorted(set(ranked), reverse=True)

# Get the length of the leaderboard to track where we are at each iteration.
    l = len(leaderboard)
    new_ranked = []    # empty array for storing our ranked results

Step 2๏ธโƒฃ: Check the player scores against a leaderboard logic.

Creating the conditional logic is the core of this solution. I couldn't figure it out this time, so I had help from HackerRank's discussion forums.๐Ÿ˜… The explanation given to the sample input above shows how the scores move up or down the leaderboard.

Basically, while the player score is greater than or equal to the current ranked score, we move one step up the ranks in the leaderboard and check the next ranked score. When the player score becomes less than a given ranked score, we add 1 to the player rank and then add this rank to our new_ranked result array.

We'll keep moving up the leaderboard until we get to the first ranked score.

# Sort the 'ranked' array so we can get a structured leaderboard.
def climbingLeaderboard(ranked, player):
    leaderboard = sorted(set(ranked), reverse=True)

# Get the length of the leaderboard to track where we are at each iteration.
    l = len(leaderboard)
    new_ranked = []    # empty array for storing our ranked results

# Loop through the 'player' array and compare against our leaderboard logic.
# 'leaderboard[l - 1]' because leaderboard is a 0 index array
    for score in player:
        while (l > 0) and (score >= leaderboard[l - 1]):        
            l -= 1                # Go up the leaderboard 
        new_ranked.append(l+1)    # increase the rank by 1 if score is less than a current rank.

    return new_ranked

climbingLeaderboard([100, 100, 50, 40, 40, 20, 10], [5, 25, 50, 120])
# [6, 4, 2, 1]

DebuggingProgrammingGIF.gif

Tip

Using vscode's debugger was especially helpful in understanding how the leaderboard logic worked. You can monitor the state of any variable and conditional logic.

If you're just beginning to code, you can't go wrong with using the debugger (as opposed to print statements). ๐Ÿ˜‰

image.png

That's it! Thanks for reading! ๐Ÿ™‚

Hope this article helped with understanding this challenge. Happy Coding!๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป

Buy me a coffee.png

ย