-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode-1244.js
40 lines (36 loc) · 887 Bytes
/
Leetcode-1244.js
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
/**
* 1244. Design A Leaderboard
* https://leetcode-cn.com/problems/design-a-leaderboard/
*/
var Leaderboard = function() {
this.board = {}
};
/**
* @param {number} playerId
* @param {number} score
* @return {void}
*/
Leaderboard.prototype.addScore = function(playerId, score) {
this.board[playerId] = (this.board[playerId] || 0) + score
};
/**
* @param {number} K
* @return {number}
*/
Leaderboard.prototype.top = function(K) {
return [...Object.values(this.board)].sort((a, b) => b-a).slice(0, K).reduce((acc, curr) => acc + curr)
};
/**
* @param {number} playerId
* @return {void}
*/
Leaderboard.prototype.reset = function(playerId) {
delete this.board[playerId]
};
/**
* Your Leaderboard object will be instantiated and called as such:
* var obj = new Leaderboard()
* obj.addScore(playerId,score)
* var param_2 = obj.top(K)
* obj.reset(playerId)
*/