403. Design a Leaderboard

You are asked to design a Leaderboard class, which supports the following functions:

  • void addScore(int playerId, int score) : Adds a score to the given playerId. If the player is already on the leaderboard, their score is incremented.
  • int top(int K): Returns the sum of the scores of the top K players.
  • void reset(int playerId): Resets the score of the given playerId to 0. It is guaranteed that the player was added before calling reset.

Example 1:

Input: operations: ["Leaderboard","addScore","addScore","addScore","addScore","addScore","top","reset","reset","addScore","top"],

values: [[],[1,73],[2,56],[3,39],[4,51],[5,4],[1],[1],[2],[2,51],[3]]

Output: [null,null,null,null,null,null,73,null,null,null,141]

Explanation:

Leaderboard leaderboard = new Leaderboard();

leaderboard.addScore(1,73); // leaderboard = [[1,73]];

leaderboard.addScore(2,56); // leaderboard = [[1,73],[2,56]];

leaderboard.addScore(3,39); // leaderboard = [[1,73],[2,56],[3,39]];

leaderboard.addScore(4,51); // leaderboard = [[1,73],[2,56],[3,39],[4,51]];

leaderboard.addScore(5,4); // leaderboard = [[1,73],[2,56],[3,39],[4,51],[5,4]];

leaderboard.top(1); // returns 73;

leaderboard.reset(1); // leaderboard = [[2,56],[3,39],[4,51],[5,4]];

leaderboard.reset(2); // leaderboard = [[3,39],[4,51],[5,4]];

leaderboard.addScore(2,51); // leaderboard = [[2,51],[3,39],[4,51],[5,4]];

leaderboard.top(3); // returns 141 = 51 + 51 + 39;

Example 2:

Input: operations : ["Leaderboard","addScore","addScore","addScore","addScore","addScore","top","reset","addScore","addScore","top"],

values: [[],[1,100],[2,90],[3,80],[4,70],[5,60],[3],[1],[1,50],[2,20],[4]]

Output: [null,null,null,null,null,null,270,null,null,null,320]

Explanation:

Leaderboard leaderboard = new Leaderboard();

Initializes an empty leaderboard.

leaderboard.addScore(1,100);

  • Player 1 is added with a score of 100.
  • Leaderboard: [[1,100]]

leaderboard.addScore(2,90);

  • Player 2 is added with a score of 90.
  • Leaderboard: [[1,100],[2,90]]

leaderboard.addScore(3,80);

  • Player 3 is added with a score of 80.
  • Leaderboard: [[1,100],[2,90],[3,80]]

leaderboard.addScore(4,70);

  • Player 4 is added with a score of 70.
  • Leaderboard: [[1,100],[2,90],[3,80],[4,70]]

leaderboard.addScore(5,60);

  • Player 5 is added with a score of 60.
  • Leaderboard: [[1,100],[2,90],[3,80],[4,70],[5,60]]

leaderboard.top(3);

  • Returns the sum of the top 3 scores: 100 + 90 + 80 = 270.

leaderboard.reset(1);

  • Player 1's score is reset.
  • Leaderboard: [[2,90],[3,80],[4,70],[5,60]]

leaderboard.addScore(1,50);

  • Player 1 is added again with a score of 50.
  • Leaderboard: [[1,50],[2,90],[3,80],[4,70],[5,60]]

leaderboard.addScore(2,20);

  • Player 2's score increases to 110 (90 + 20).
  • Leaderboard: [[1,50],[2,110],[3,80],[4,70],[5,60]]

leaderboard.top(4);

  • Returns the sum of the top 4 scores: 110 + 80 + 70 + 60 = 320.

Now Your Turn!

Pick the correct output for the given input

Input : operations: ["Leaderboard","addScore","addScore","addScore","top","reset","addScore","top","reset","top"]

values: [[],[1,40],[2,35],[3,25],[2],[2],[2,50],[2],[1],[1]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= playerId, K <= 104
  • It's guaranteed that K is less than or equal to the current number of players.
  • 1 <= score <= 100
  • There will be at most 1000 function calls.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Leaderboard {
public:
Leaderboard() {
}
void addScore(int playerId, int score) {
}
int top(int K) {
}
void reset(int playerId) {
}
};
 
/**
* Your Leaderboard object will be instantiated and called as such:
* Leaderboard* obj = new Leaderboard();
* obj->addScore(playerId,score);
* int param_2 = obj->top(K);
* obj->reset(playerId);
*/
Test Case

Input:

Input1
Input2