forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1244.js
More file actions
39 lines (36 loc) · 716 Bytes
/
Copy path1244.js
File metadata and controls
39 lines (36 loc) · 716 Bytes
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
var Leaderboard = function() {
this.d = {};
};
/**
* @param {number} playerId
* @param {number} score
* @return {void}
*/
Leaderboard.prototype.addScore = function(playerId, score) {
if (playerId in this.d) {
this.d[playerId] += score;
} else this.d[playerId] = score;
};
/**
* @param {number} K
* @return {number}
*/
Leaderboard.prototype.top = function(K) {
let q = []
for (let i in this.d) {
q.push(this.d[i]);
}
q.sort((a, b) => b - a);
let res = 0;
while (K) {
res += q[--K];
}
return res;
};
/**
* @param {number} playerId
* @return {void}
*/
Leaderboard.prototype.reset = function(playerId) {
delete this.d[playerId];
};