SDE Path

Design a Leaderboard

Medium

Design a Leaderboard

Design a game leaderboard: accumulate players' scores, sum the current top-K, and drop a player — LeetCode's Design A Leaderboard.

Requirements

  • addScore(playerId, score) accumulates — it adds to the player's running total, it does not replace it. An unseen player is created at 0 first, then score is added.
  • top(k) returns the sum of the k largest current totals. If the board has fewer than k players, sum every player; on an empty board the answer is 0.
  • reset(playerId) removes the player from the board (this is the chosen semantics). Their total is gone, not zeroed-in-place — but a subsequent addScore for that id recreates the player starting from 0.
  • All operations are deterministic and depend only on the commands seen so far; there is no time and no randomness.

API to implement

Implement the class Leaderboard. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | addScore <playerId> <score> | — | Add score to playerId's running total. If the player is not on the board yet, create them starting at 0, then add. | | top <k> | int | Return the sum of the k highest current totals. If fewer than k players exist, sum all of them (an empty board returns 0). | | reset <playerId> | — | Remove playerId from the leaderboard entirely. A later addScore for that player recreates them starting again at 0. |

Input format

The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
6
addScore 1 50
addScore 2 30
addScore 1 20
top 1
top 2
top 5
Expected output
70
100
100
Example 2
Input
8
addScore 1 40
addScore 2 60
addScore 3 50
top 2
reset 2
top 2
addScore 2 10
top 3
Expected output
110
90
100