284. Kth Largest Element in a Stream

EasyHeap

You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores. You are tasked with implementing a class KthLargest that, for a given integer k, maintains a stream of test scores and continuously returns the kth highest test score after a new score has been submitted. Implement the KthLargest class: - KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of test scores nums. - int add(int val) Adds a new test score val to the stream and returns the element representing the kth largest element in the pool of test scores so far.

Examples

Input: ["KthLargest","add","add","add","add","add"] [[3,[4,5,8,2]],[3],[5],[10],[9],[4]]

Output: [null,4,5,5,8,8]

Explanation: Each add returns the 3rd largest element seen so far.

Constraints

  • 0 <= nums.length <= 10^4
  • 1 <= k <= nums.length + 1
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= val <= 10^4
  • At most 10^4 calls will be made to add.
Loading...

Run checks all cases above. Submit evaluates all test cases.