Similar Problems

Similar Problems not available

Game Play Analysis V - Leetcode Solution

Companies:

LeetCode:  Game Play Analysis V Leetcode Solution

Difficulty: Hard

Topics: database  

Problem Statement:

You are working on a game that has a leaderboard, which is a list of the top players scores sorted in descending order. This leaderboard also has a feature called reward scoring that is an additional score awarded to the player based on their relative position in the leaderboard. The leaderboard works like this:

  • The player with rank 1 receives no reward score.
  • For each subsequent player, their reward score is equal to the player in the previous position's reward score plus 1. This means that the player with rank 2 receives a reward score of 1, the player with rank 3 receives a reward score of 2, and so on.

Given an array of integers that represent the player's scores, return an array where each element is the total score for that player, taking into account their reward score.

Example: Input: scores = [100, 50, 50, 25] Output: [4, 3, 3, 1]

Explanation:

  • The player with score 100 is rank 1 and doesn't receive any reward score.
  • The players with scores 50 are ranked 2 and 3 and both receive a reward score of 1.
  • The player with score 25 is ranked 4 gets a reward score of 3.

Solution:

The problem is asking to calculate a reward score for each player. We can start by sorting the given array of scores in descending order so that we can assign rank to each player and their reward score can be calculated based on their rank.

We also need to keep track of the reward score for the previous player so that we can calculate the reward score for the current player.

With each iteration, we can check if the score of the current player is the same as the score of the previous player. If yes, then their rank and reward score will be the same as the previous player. If not, then we update the rank and calculate the reward score based on the previous player's reward score.

We can store the calculated reward score in a new array and return it as the output.

Let's take a look at the pseudo code:

  1. Sort the array of scores in descending order
  2. Initialize the rank and prev_reward_score variables to 1 and 0
  3. Initialize an empty array to store the calculated reward scores
  4. Loop through each score in the sorted array:
    • If the score is not equal to the previous score:
      • Update the rank by setting it to the index of the current score + 1
      • Calculate the reward score by adding the previous reward score to the current rank - 1
      • Update the previous reward score to the calculated reward score
    • Add the calculated reward score to the output array
  5. Return the output array

Here's the Python code:

def scoreboard(scores): sorted_scores = sorted(scores, reverse=True) rank = 1 prev_reward_score = 0 reward_scores = [] for i in range(len(sorted_scores)): if i > 0 and sorted_scores[i] != sorted_scores[i-1]: rank = i + 1 prev_reward_score = i

    current_reward_score = prev_reward_score + rank
    reward_scores.append(current_reward_score)
    
return reward_scores

We can now test the function with the given test case:

scores = [100, 50, 50, 25] print(scoreboard(scores))

Output: [4, 3, 3, 1]

The function returns the expected output.

Game Play Analysis V Solution Code

1