-
Notifications
You must be signed in to change notification settings - Fork 2
/
LeaderBoard.java
51 lines (41 loc) · 1.46 KB
/
LeaderBoard.java
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
package Leetcode;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
/**
* @author kalpak
*
* 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.
*/
public class LeaderBoard {
Map<Integer, Integer> leaderBoard;
public LeaderBoard() {
leaderBoard = new HashMap<>();
}
public void addScore(int playerId, int score) {
leaderBoard.put(playerId, leaderBoard.getOrDefault(playerId, 0) + score);
}
public int top(int K) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
int scoreSum = 0;
for(int score : leaderBoard.values()) {
minHeap.offer(score);
if(minHeap.size() > K)
minHeap.poll();
}
while(!minHeap.isEmpty())
scoreSum += minHeap.poll();
return scoreSum;
}
public void reset(int playerId) {
leaderBoard.remove(playerId);
}
}