Leaderboard Cut-Off

Easy · Heaps & Top-K · about 15 min

An online game gives a prize to the top k scores. Scores arrive one at a time, and after each new score the game shows the cut-off: the k-th highest score seen so far. Equal scores count separately, so with scores [7, 7] and k = 2 the cut-off is 7.

Return a list with one entry per score: the cut-off right after that score arrives, or -1 if fewer than k scores have arrived yet.

  • Every score is >= 0 and k >= 1.

Example: kth_best_so_far([40, 10, 50, 30, 60], 3) → [-1, -1, 10, 30, 40]

Hints

  • You only care about the k best scores. Anything below the k-th best can never matter again.
  • A min-heap of size k keeps the smallest of those k best on top, which is exactly the cut-off.
  • After pushing a score, pop once if the heap holds more than k scores. Report the top only when it holds exactly k.

Learn the pattern first: the Heaps & Top-K lesson.

More Heaps & Top-K problems