Design a Leaderboard class, which has 3 functions:
addScore(playerId, score): Update the leaderboard by adding score to the given player’s score. If there is no player with such id in the leaderboard, add him to the leaderboard with the given score.
top(K): Return the score sum of the top K players.
reset(playerId): Reset the score of the player with the given id to 0 (in other words erase it from the leaderboard). It is guaranteed that the player was added to the leaderboard before calling this function.
/** * 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); */
classLeaderboard { unordered_map<int, int> m; multiset<int> ms; public: Leaderboard() {
}
voidaddScore(int p, int s){ auto it = ms.find(m[p]); if(it != ms.end()) ms.erase(it); m[p] += s; ms.insert(m[p]); }
inttop(int K){ intres(0), i(0); for(auto it = ms.rbegin(); it != ms.rend() && i < K; it++) { res += *it; i++; } return res; }
voidreset(int p){ auto it = ms.find(m[p]); if(it != ms.end()) ms.erase(it); m[p] = 0; } };
/** * 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); */