Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

30:00

Kth Largest Element in a Stream
easy
Topics
Companies

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:
Input: {"commands":["KthLargest","add","add","add","add","add"],"values":[[3,[4,5,8,2]],[3],[5],[10],[9],[4]]}
Output: [null,4,5,5,8,8]
Constraints:
  • 1k1041 \leq k \leq 10^4

  • 0nums.length1040 \leq \text{nums.length} \leq 10^4

  • 104nums[i]104-10^4 \leq \text{nums}[i] \leq 10^4

  • 104val104-10^4 \leq \text{val} \leq 10^4

  • At most 10410^4 calls will be made to add.

  • It is guaranteed that there will be at least kk elements in the array when you call add.

Input
arr ={"commands":["KthLargest","add","add","add","add","add"],"values":[[3,[4,5,8,2]],[3],[5],[10],[9],[4]]}

Init K=3, nums=[4,5,8,2]

Max Heap (Left)

Empty

Min Heap (Right)

Empty

Variables
No variables to display
DepthFunction Call
Stack empty
0/21