-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1244-design-a-leaderboard.cpp
57 lines (43 loc) · 1.14 KB
/
1244-design-a-leaderboard.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// class Leaderboard {
// public:
// Leaderboard() {
// }
// void addScore(int playerId, int score) {
// }
// int top(int K) {
// }
// void reset(int 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);
*/
class Leaderboard {
public:
Leaderboard() {}
void addScore(int playerId, int score) {
sortedVal.erase({mapping[playerId], playerId});
mapping[playerId] += score;
sortedVal.insert({mapping[playerId], playerId});
}
int top(int k) {
int res = 0, c = 0;
for (auto it = sortedVal.rbegin(); it != sortedVal.rend()
&& c < k; it++) {
res += (it->first);
c++;
}
return res;
}
void reset(int playerId) {
sortedVal.erase({mapping[playerId], playerId});
mapping[playerId] = 0;
}
private:
unordered_map<int, int> mapping;
set<pair<int, int>> sortedVal;
};