-
Notifications
You must be signed in to change notification settings - Fork 2
/
1244.design-a-leaderboard.java
48 lines (39 loc) · 1.14 KB
/
1244.design-a-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
/*
* @lc app=leetcode id=1244 lang=java
*
* [1244] Design A Leaderboard
*/
// @lc code=start
class Leaderboard {
Map<Integer, Integer> player;
public Leaderboard() {
player = new HashMap<>();
}
public void addScore(int playerId, int score) {
player.put(playerId, player.getOrDefault(playerId, 0) + score);
}
public int top(int K) {
PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
for (Map.Entry<Integer, Integer> entry : player.entrySet()) {
heap.add(entry);
}
int result = 0;
while (K > 0 && !heap.isEmpty()) {
Map.Entry<Integer, Integer> entry = heap.poll();
result += entry.getValue();
K--;
}
return result;
}
public void reset(int playerId) {
player.put(playerId, 0);
}
}
/**
* Your Leaderboard object will be instantiated and called as such:
* Leaderboard obj = new Leaderboard();
* obj.addScore(playerId,score);
* int param_2 = obj.top(K);
* obj.reset(playerId);
*/
// @lc code=end