Leetcode 1176 Solution
This article provides solution to leetcode question 1176 (design-a-leaderboard)
Access this page by simply typing in "lcs 1176" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/design-a-leaderboard
Solution
class Leaderboard {
std::multiset<std::pair<int, int>> s;
std::unordered_map<int, int> scores;
public:
Leaderboard() {
}
void addScore(int playerId, int score) {
if (scores.find(playerId) != scores.end())
{
s.erase(std::make_pair(scores[playerId], playerId));
int new_score = score + scores[playerId];
scores[playerId] = new_score;
s.insert(std::make_pair(new_score, playerId));
}
else
{
scores[playerId] = score;
s.insert(std::make_pair(score, playerId));
}
}
int top(int K) {
int sum = 0;
int i = 0;
for (auto it = s.rbegin(); it != s.rend() && i < K; it++, i++)
sum += it->first;
return sum;
}
void reset(int playerId) {
s.erase(std::make_pair(scores[playerId], playerId));
scores.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);
*/