-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1244-design-a-leaderboard.js
More file actions
47 lines (42 loc) · 1.34 KB
/
1244-design-a-leaderboard.js
File metadata and controls
47 lines (42 loc) · 1.34 KB
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
/**
* 1244. Design A Leaderboard
* https://leetcode.com/problems/design-a-leaderboard/
* Difficulty: Medium
*
* Design a Leaderboard class, which has 3 functions:
* - addScore(playerId, score): Update the leaderboard by adding score to the given player's
* score. If there is no player with such id in the leaderboard, add him to the leaderboard
* with the given score.
* - top(K): Return the score sum of the top K players.
* - 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.
*/
var Leaderboard = function() {
this.scores = new Map();
};
/**
* @param {number} playerId
* @param {number} score
* @return {void}
*/
Leaderboard.prototype.addScore = function(playerId, score) {
this.scores.set(playerId, (this.scores.get(playerId) || 0) + score);
};
/**
* @param {number} K
* @return {number}
*/
Leaderboard.prototype.top = function(K) {
const sortedScores = Array.from(this.scores.values()).sort((a, b) => b - a);
return sortedScores.slice(0, K).reduce((sum, score) => sum + score, 0);
};
/**
* @param {number} playerId
* @return {void}
*/
Leaderboard.prototype.reset = function(playerId) {
this.scores.delete(playerId);
};