-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem-1244.cpp
44 lines (37 loc) · 954 Bytes
/
Problem-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
41
42
43
44
//Problem - 1244
// https://leetcode.com/problems/design-a-leaderboard/
// O(nlogk) for top() rest O(1) time complexity and O(n) space complexity sol
class Leaderboard {
public:
unordered_map <int, int> um;
Leaderboard() {
um.clear();
}
void addScore(int playerId, int score) {
um[playerId] += score;
}
int top(int K) {
priority_queue <int, vector <int>, greater <int>> pq;
int ctr = 0;
for(auto itr : um) {
if(ctr < K)
pq.push(itr.second);
else {
if(itr.second > pq.top()) {
pq.pop();
pq.push(itr.second);
}
}
ctr++;
}
int sum = 0;
while(!pq.empty()) {
sum += pq.top();
pq.pop();
}
return sum;
}
void reset(int playerId) {
um[playerId] = 0;
}
};