-
Notifications
You must be signed in to change notification settings - Fork 0
/
1244-Design A Leaderboard.cpp
51 lines (46 loc) · 1.35 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
class Leaderboard {
public:
Leaderboard() {
}
void addScore(int playerId, int score)
{
if(!leaderBoard.count(playerId))
leaderBoard[playerId]=score,rLeaderBoard.insert(make_pair(score,playerId));
else
{
it=rLeaderBoard.find(leaderBoard[playerId]);
while(it->second!=playerId)
it++;
rLeaderBoard.erase(it);
rLeaderBoard.insert(make_pair(leaderBoard[playerId]+score,playerId));
leaderBoard[playerId]+=score;
}
}
int top(int K)
{
int sum=0;
for(it2=rLeaderBoard.rbegin();it2!=rLeaderBoard.rend()&&K;it2++,K--)
sum+=it2->first;
return sum;
}
void reset(int playerId)
{
it=rLeaderBoard.lower_bound(leaderBoard[playerId]);
while(it!=rLeaderBoard.end()&&it->second!=playerId)
it++;
rLeaderBoard.erase(it);
leaderBoard.erase(playerId);
}
private:
multimap<int,int> rLeaderBoard;
multimap<int,int>::iterator it;
multimap<int,int>::reverse_iterator it2;
unordered_map<int,int> leaderBoard;
};
/**
* 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);
*/