Skip to the code
cf.
+10 XP

#382 Live Leaderboard Cutoff

easy

A game's lobby shows the score you need to reach the top k: the k-th highest score so far, counting equal scores separately. New scores keep arriving.

Write a class LeaderboardCutoff with:

  • constructor(k, scores): scores holds the scores so far.
  • add(score): records a new score and returns the current k-th highest score. There will always be at least k scores after each add.

Each example lists the calls in order, then each call's arguments. The first call creates the object with new LeaderboardCutoff(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["LeaderboardCutoff","add","add","add"]
[[3,[40,50,20,80]],[30],[90],[60]]
Output: [null,40,50,60]

Top 3 after adding 30: 80, 50, 40. After 90: 90, 80, 50. After 60: 90, 80, 60.

Example 2

Input: ["LeaderboardCutoff","add","add"]
[[1,[]],[5],[3]]
Output: [null,5,5]

With k = 1, the cutoff is the best score so far.

Constraints

  • 1 <= k <= 10^4
  • 0 <= scores.length <= 10^4
  • At most 10^4 calls to add.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).