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.
Implement KthLargest class:
- KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
- int add(int val) Returns the element representing the kth largest element in the stream.
|
|
Constraints:
- 1 <= k <= 104
- 0 <= nums.length <= 104
- -104 <= nums[i] <= 104
- -104 <= val <= 104
- At most 104 calls will be made to add.
- It is guaranteed that there will be at least k elements in the array when you search for the kth element.
|
|
Solution 2021-11-20
|
|
Notes:
- Ask about unique value
- Ex1: 6,5,4,2 and k = 3 will return 4
- Ex2: 6,5,5,2 and k = 3 will return 4 or 5?