Design A Leaderboard Problem
#1244
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 1244.
Design a Leaderboard class, which has 3 functions:
addScore(playerId, score): Update the leaderboard by addingscoreto the given player's score. If there is no player with such id in the leaderboard, add him to the leaderboard with the givenscore.top(K): Return the score sum of the topKplayers.reset(playerId): Reset the score of the player with the given id to 0 (in other words erase it from the leaderboard). It is guaranteed that the player was added to the leaderboard before calling this function.
Initially, the leaderboard is empty.
Example 1:
Input:
["Leaderboard","addScore","addScore","addScore","addScore","addScore","top","reset","reset","addScore","top"]
[[],[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;
Constraints:
1 <= playerId, K <= 10000- It's guaranteed that
Kis less than or equal to the current number of players. 1 <= score <= 100- There will be at most
1000function calls.
Solution
Approach: Hash Map, Sort on top
Keep a hash map from playerId to the player's score.
addScore(playerId, score): addscoreto the player's entry, creating it with0first if needed.reset(playerId): remove the player from the map. Removing is better than storing0: a reset player is no longer on the leaderboard, and leaving a zero entry would make the map grow.top(K): collect the scores, sort them in descending order, and add up the firstK.Kis guaranteed to be at most the number of players.
There are at most 1000 calls, so at most 1000 players, and sorting on each top call is cheap. With many more calls, keep the scores in an ordered multiset (a balanced tree such as Java's TreeMap of score to count) as well, so that addScore and reset update it in O(log n), and top walks only the K largest scores.
Complexity: addScore and reset take O(1). top takes O(n log n) for n players. The space is O(n).
class Leaderboard:
def __init__(self):
self.score = {} # playerId -> score; a reset player is removed
def addScore(self, playerId: int, score: int) -> None:
self.score[playerId] = self.score.get(playerId, 0) + score
def top(self, K: int) -> int:
return sum(sorted(self.score.values(), reverse=True)[:K])
def reset(self, playerId: int) -> None:
del self.score[playerId]