fromsortedcontainersimportSortedListclassLeaderboard:def__init__(self):self.d=defaultdict(int)self.rank=SortedList()defaddScore(self,playerId:int,score:int)->None:ifplayerIdnotinself.d:self.d[playerId]=scoreself.rank.add(score)else:self.rank.remove(self.d[playerId])self.d[playerId]+=scoreself.rank.add(self.d[playerId])deftop(self,K:int)->int:returnsum(self.rank[-K:])defreset(self,playerId:int)->None:self.rank.remove(self.d.pop(playerId))# Your Leaderboard object will be instantiated and called as such:# obj = Leaderboard()# obj.addScore(playerId,score)# param_2 = obj.top(K)# obj.reset(playerId)
classLeaderboard{privateMap<Integer,Integer>d=newHashMap<>();privateTreeMap<Integer,Integer>rank=newTreeMap<>((a,b)->b-a);publicLeaderboard(){}publicvoidaddScore(intplayerId,intscore){d.merge(playerId,score,Integer::sum);intnewScore=d.get(playerId);if(newScore!=score){rank.merge(newScore-score,-1,Integer::sum);}rank.merge(newScore,1,Integer::sum);}publicinttop(intK){intans=0;for(vare:rank.entrySet()){intscore=e.getKey(),cnt=e.getValue();cnt=Math.min(cnt,K);ans+=score*cnt;K-=cnt;if(K==0){break;}}returnans;}publicvoidreset(intplayerId){intscore=d.remove(playerId);if(rank.merge(score,-1,Integer::sum)==0){rank.remove(score);}}}/** * 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{public:Leaderboard(){}voidaddScore(intplayerId,intscore){d[playerId]+=score;intnewScore=d[playerId];if(newScore!=score){rank.erase(rank.find(newScore-score));}rank.insert(newScore);}inttop(intK){intans=0;for(auto&x:rank){ans+=x;if(--K==0){break;}}returnans;}voidreset(intplayerId){intscore=d[playerId];d.erase(playerId);rank.erase(rank.find(score));}private:unordered_map<int,int>d;multiset<int,greater<int>>rank;};/** * 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); */
usestd::collections::BTreeMap;#[allow(dead_code)]structLeaderboard{/// This also keeps track of the top K players since it's implicitly sortedrecord_map:BTreeMap<i32,i32>,}implLeaderboard{#[allow(dead_code)]fnnew()->Self{Self{record_map:BTreeMap::new(),}}#[allow(dead_code)]fnadd_score(&mutself,player_id:i32,score:i32){ifself.record_map.contains_key(&player_id){// The player exists, just add the scoreself.record_map.insert(player_id,self.record_map.get(&player_id).unwrap()+score);}else{// Add the new player to the mapself.record_map.insert(player_id,score);}}#[allow(dead_code)]fntop(&self,k:i32)->i32{letmutcur_vec:Vec<(i32,i32)>=self.record_map.iter().map(|(k,v)|(*k,*v)).collect();cur_vec.sort_by(|lhs,rhs|rhs.1.cmp(&lhs.1));// Iterate reversely for Kletmutsum=0;letmuti=0;for(_,value)in&cur_vec{ifi==k{break;}sum+=value;i+=1;}sum}#[allow(dead_code)]fnreset(&mutself,player_id:i32){// The player is ensured to exist in the board// Just set the score to 0self.record_map.insert(player_id,0);}}