1244. Design A Leaderboard π
SourceBiweekly Contest 12 Q1DifficultyMediumRating1354
Description
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.
Solutions
Solution 1: Hash Table + Ordered List
Thinking
We update and reset a player's score and sum the top \(K\) scores. A thousand calls make a linear scan of all players unattractive. A hash map stores the score by \(\textit{playerId}\); a sorted list holds the multiset of scores.
Adding a score removes the old value then inserts the new one; reset deletes symmetrically; \(\textit{top}(K)\) sums the last \(K\) entries. The map locates a player in \(O(1)\); the sorted list updates in logarithmic time and exposes an ordered suffix.
We use a hash table \(d\) to record the scores of each player, and an ordered list \(\textit{rank}\) to record the scores of all players.
When the \(\textit{addScore}\) function is called, we first check if the player is in the hash table \(d\). If not, we add their score to the ordered list \(\textit{rank}\). Otherwise, we first remove their score from the ordered list \(\textit{rank}\), then add their updated score to the ordered list \(\textit{rank}\), and finally update the score in the hash table \(d\). The time complexity is \(O(\log n)\).
When the \(\textit{top}\) function is called, we directly return the sum of the first \(K\) elements in the ordered list \(\textit{rank}\). The time complexity is \(O(K \log n)\).
When the \(\textit{reset}\) function is called, we first remove the player from the hash table \(d\), then remove their score from the ordered list \(\textit{rank}\). The time complexity is \(O(\log n)\).
The space complexity is \(O(n)\), where \(n\) is the number of players.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |