-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to leetcode problem: No.1244. Design A Leaderboard
- Loading branch information
Showing
4 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
solution/1200-1299/1244.Design A Leaderboard/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Leaderboard { | ||
private Map<Integer, Integer> 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<Integer> 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); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |