[
hash table
design
sort
]
Leetcode 1244 Design A Leaderboard
Problem statement
https://leetcode.com/problems/tournament-winners/
Solution
All we need to do is to keep score for each person, so we can quickly update it.
Complexity
Time complexity for init, reset, addScore
is O(1)
, time complexity for top
is O(n log k)
Code
class Leaderboard:
def __init__(self):
self.scores = Counter() #id -> score
def addScore(self, playerId, score):
self.scores[playerId] += score
def top(self, K):
return sum(i[1] for i in self.scores.most_common(K))
def reset(self, playerId):
self.scores[playerId] = 0
Remark
I think time complexity for top
can be improved, one way to do it is to use that scores are not so big and use bucket sort.