Skip to content

Commit e29a178

Browse files
committed
1244. Design A Leaderboard
1 parent 29cd4b1 commit e29a178

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

design-a-leaderboard.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Leaderboard {
2+
public:
3+
4+
unordered_map<int, int> scores;
5+
set<pair<int, int> > pairs;
6+
7+
Leaderboard() {
8+
9+
}
10+
11+
void addScore(int playerId, int score) {
12+
pairs.erase({scores[playerId], playerId});
13+
scores[playerId] += score;
14+
pairs.insert({scores[playerId], playerId});
15+
}
16+
17+
int top(int K) {
18+
int res = 0;
19+
int i = 0;
20+
for (auto it = pairs.rbegin(); it != pairs.rend() && i < K; it++, i++) {
21+
res += it->first;
22+
}
23+
return res;
24+
}
25+
void reset(int playerId) {
26+
pairs.erase({scores[playerId], playerId});
27+
scores[playerId] = 0;
28+
}
29+
};

0 commit comments

Comments
 (0)