diff --git a/solution/1200-1299/1244.Design A Leaderboard/README.md b/solution/1200-1299/1244.Design A Leaderboard/README.md index 5187136391d37..2700abd8496a5 100644 --- a/solution/1200-1299/1244.Design A Leaderboard/README.md +++ b/solution/1200-1299/1244.Design A Leaderboard/README.md @@ -12,7 +12,6 @@
  1. addScore(playerId, score): - - ## 解法 +用哈希表存放每个 playerId 所对应的分数。 + +计算 topK 时,取出所有的分数,进行排序,获取前 K 个分数,累加得到结果。 + ### **Python3** @@ -72,7 +74,27 @@ leaderboard.top(3); // returns 141 = 51 + 51 + 39; ```python +class Leaderboard: + + def __init__(self): + self.player_scores = {} + def addScore(self, playerId: int, score: int) -> None: + self.player_scores[playerId] = self.player_scores.get(playerId, 0) + score + + def top(self, K: int) -> int: + scores = sorted(list(self.player_scores.values()), reverse=True) + return sum(scores[:K]) + + def reset(self, playerId: int) -> None: + self.player_scores[playerId] = 0 + + +# Your Leaderboard object will be instantiated and called as such: +# obj = Leaderboard() +# obj.addScore(playerId,score) +# param_2 = obj.top(K) +# obj.reset(playerId) ``` ### **Java** @@ -80,7 +102,39 @@ leaderboard.top(3); // returns 141 = 51 + 51 + 39; ```java - +class Leaderboard { + private Map playerScores; + + public Leaderboard() { + playerScores = new HashMap<>(); + } + + public void addScore(int playerId, int score) { + playerScores.put(playerId, playerScores.getOrDefault(playerId, 0) + score); + } + + public int top(int K) { + List scores = new ArrayList<>(playerScores.values()); + Collections.sort(scores, Collections.reverseOrder()); + int res = 0; + for (int i = 0; i < K; ++i) { + res += scores.get(i); + } + return res; + } + + public void reset(int playerId) { + playerScores.put(playerId, 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); + */ ``` ### **...** diff --git a/solution/1200-1299/1244.Design A Leaderboard/README_EN.md b/solution/1200-1299/1244.Design A Leaderboard/README_EN.md index 09fe1301fa9d5..cab7daeb3a066 100644 --- a/solution/1200-1299/1244.Design A Leaderboard/README_EN.md +++ b/solution/1200-1299/1244.Design A Leaderboard/README_EN.md @@ -48,7 +48,6 @@ leaderboard.top(3); // returns 141 = 51 + 51 + 39;
  2. There will be at most 1000 function calls.
  3. - ## Solutions @@ -56,13 +55,65 @@ leaderboard.top(3); // returns 141 = 51 + 51 + 39; ### **Python3** ```python +class Leaderboard: + + def __init__(self): + self.player_scores = {} + + def addScore(self, playerId: int, score: int) -> None: + self.player_scores[playerId] = self.player_scores.get(playerId, 0) + score + + def top(self, K: int) -> int: + scores = sorted(list(self.player_scores.values()), reverse=True) + return sum(scores[:K]) + def reset(self, playerId: int) -> None: + self.player_scores[playerId] = 0 + + +# Your Leaderboard object will be instantiated and called as such: +# obj = Leaderboard() +# obj.addScore(playerId,score) +# param_2 = obj.top(K) +# obj.reset(playerId) ``` ### **Java** ```java - +class Leaderboard { + private Map playerScores; + + public Leaderboard() { + playerScores = new HashMap<>(); + } + + public void addScore(int playerId, int score) { + playerScores.put(playerId, playerScores.getOrDefault(playerId, 0) + score); + } + + public int top(int K) { + List scores = new ArrayList<>(playerScores.values()); + Collections.sort(scores, Collections.reverseOrder()); + int res = 0; + for (int i = 0; i < K; ++i) { + res += scores.get(i); + } + return res; + } + + public void reset(int playerId) { + playerScores.put(playerId, 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); + */ ``` ### **...** diff --git a/solution/1200-1299/1244.Design A Leaderboard/Solution.java b/solution/1200-1299/1244.Design A Leaderboard/Solution.java new file mode 100644 index 0000000000000..6e1953d92404e --- /dev/null +++ b/solution/1200-1299/1244.Design A Leaderboard/Solution.java @@ -0,0 +1,33 @@ +class Leaderboard { + private Map playerScores; + + public Leaderboard() { + playerScores = new HashMap<>(); + } + + public void addScore(int playerId, int score) { + playerScores.put(playerId, playerScores.getOrDefault(playerId, 0) + score); + } + + public int top(int K) { + List scores = new ArrayList<>(playerScores.values()); + Collections.sort(scores, Collections.reverseOrder()); + int res = 0; + for (int i = 0; i < K; ++i) { + res += scores.get(i); + } + return res; + } + + public void reset(int playerId) { + playerScores.put(playerId, 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); + */ \ No newline at end of file diff --git a/solution/1200-1299/1244.Design A Leaderboard/Solution.py b/solution/1200-1299/1244.Design A Leaderboard/Solution.py new file mode 100644 index 0000000000000..74670a4a267e2 --- /dev/null +++ b/solution/1200-1299/1244.Design A Leaderboard/Solution.py @@ -0,0 +1,21 @@ +class Leaderboard: + + def __init__(self): + self.player_scores = {} + + def addScore(self, playerId: int, score: int) -> None: + self.player_scores[playerId] = self.player_scores.get(playerId, 0) + score + + def top(self, K: int) -> int: + scores = sorted(list(self.player_scores.values()), reverse=True) + return sum(scores[:K]) + + def reset(self, playerId: int) -> None: + self.player_scores[playerId] = 0 + + +# Your Leaderboard object will be instantiated and called as such: +# obj = Leaderboard() +# obj.addScore(playerId,score) +# param_2 = obj.top(K) +# obj.reset(playerId)