Design a class to find the kth largest element in a stream. Note that it is the kth largest element in sorted order, not the kth distinct element.
Implement the KthLargest class:
KthLargest(k, nums) — Initializes the object with the integer k and the stream of integers nums.add(val) — Appends the integer val to the stream and returns the element representing the kth largest element in the stream.["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]][null, 4, 5, 5, 8, 8]1 <= k <= 10^40 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4-10^4 <= val <= 10^4At most 10^4 calls will be made to addIt is guaranteed that there will be at least k elements in the array when you search for the kth elementExpected time complexity: O(log k) per addRun your code to see results
Use Cmd+Enter to run