703. Kth Largest Element in a Stream
703. Kth Largest Element in a Stream
Description
Difficulty: Easy
Related Topics: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data 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.
Implement KthLargest
class:
KthLargest(int k, int[] nums)
Initializes the object with the integerk
and the stream of integersnums
.int add(int val)
Appends the integerval
to the stream and returns the element representing the kth largest element in the stream.
Example 1:
1 | Input |
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.
Hints/Notes
- 2023/10/17
- priority queue
- No solution from 0x3F
Solution
Language: C++
1 | class KthLargest { |