easy

Kth Largest Element in a Stream

Design a class initialized with k and an initial list of scores. Each call to add(val) appends a new score and returns the k-th largest score seen so far.

Constraints
  • 1 ≤ k ≤ 10^4
  • 0 ≤ initial length ≤ 10^4
  • At most 10^4 calls to add
  • There are at least k elements whenever add returns
Examples
in: k = 3, nums = [4,5,8,2]; add(3), add(5), add(10), add(9), add(4)
out: 4, 5, 5, 8, 8

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.