> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Design A Leaderboard Python Solution

> Tested Python solution for LeetCode 1244 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 1244, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Hash Table](/catalog/topics/hash-table), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/design-a-leaderboard/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1244   # by problem number
lcpy gen -s design_a_leaderboard   # by problem name
```

## Problem

Design a Leaderboard class, which has 3 functions:

* `addScore(playerId, score)`: Update the leaderboard by adding `score` to the given player's score. If there is no player with such id in the leaderboard, add him to the leaderboard with the given `score`.
* `top(K)`: Return the score sum of the top `K` players.
* `reset(playerId)`: Reset the score of the player with the given id to 0 (in other words erase it from the leaderboard). It is guaranteed that the player was added to the leaderboard before calling this function.

Initially, the leaderboard is empty.

### Examples

```
Input:
["Leaderboard","addScore","addScore","addScore","addScore","addScore","top","reset","reset","addScore","top"]
[[],[1,73],[2,56],[3,39],[4,51],[5,4],[1],[1],[2],[2,51],[3]]
Output:
[null,null,null,null,null,null,73,null,null,null,141]

Explanation
Leaderboard leaderboard = new Leaderboard();
leaderboard.addScore(1,73);   // leaderboard = [[1,73]];
leaderboard.addScore(2,56);   // leaderboard = [[1,73],[2,56]];
leaderboard.addScore(3,39);   // leaderboard = [[1,73],[2,56],[3,39]];
leaderboard.addScore(4,51);   // leaderboard = [[1,73],[2,56],[3,39],[4,51]];
leaderboard.addScore(5,4);    // leaderboard = [[1,73],[2,56],[3,39],[4,51],[5,4]];
leaderboard.top(1);           // returns 73;
leaderboard.reset(1);         // leaderboard = [[2,56],[3,39],[4,51],[5,4]];
leaderboard.reset(2);         // leaderboard = [[3,39],[4,51],[5,4]];
leaderboard.addScore(2,51);   // leaderboard = [[2,51],[3,39],[4,51],[5,4]];
leaderboard.top(3);           // returns 141 = 51 + 51 + 39;
```

### Constraints

* `1 <= playerId, K <= 10^4`
* It's guaranteed that `K` is less than or equal to the current number of players.
* `1 <= score <= 100`
* At most `1000` function calls will be made.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_a_leaderboard/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_a_leaderboard/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heapq


class Leaderboard:
    # Time: add_score O(1), top O(n log k), reset O(1)
    # Space: O(n) where n is the number of players on the leaderboard
    def __init__(self) -> None:
        self.scores: dict[int, int] = {}

    # Time: O(1)
    # Space: O(1)
    def add_score(self, player_id: int, score: int) -> None:
        self.scores[player_id] = self.scores.get(player_id, 0) + score

    # Time: O(n log k)
    # Space: O(k)
    def top(self, k: int) -> int:
        return sum(heapq.nlargest(k, self.scores.values()))

    # Time: O(1)
    # Space: O(1)
    def reset(self, player_id: int) -> None:
        del self.scores[player_id]
```

## Complexity

| Time | Space |
| - | - |
| add\_score O(1), top O(n log k), reset O(1) | O(n) where n is the number of players on the leaderboard |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.