-
Notifications
You must be signed in to change notification settings - Fork 3
/
1244.cpp
40 lines (35 loc) · 943 Bytes
/
1244.cpp
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
class Leaderboard {
public:
multiset<int, greater<int>> mst;
unordered_map<int, int> id2score;
Leaderboard() {
}
void addScore(int playerId, int score) {
if (id2score.find(playerId) != id2score.end()) {
mst.erase(mst.find(id2score[playerId]));
}
id2score[playerId] += score;
mst.insert(id2score[playerId]);
}
int top(int K) {
auto it = mst.begin();
int sum = 0;
while (K--) {
sum += *it;
it++;
if (it == mst.end()) break;
}
return sum;
}
void reset(int playerId) {
mst.erase(mst.find(id2score[playerId]));
id2score.erase(playerId);
}
};
/**
* 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);
*/