703. 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.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) Appends the integer val to the stream and returns the element representing the kth largest element in the stream.
优先级队列,插入所有元素,小元素在前。
当队列长度大于k时,poll掉前面的元素。
1 | class KthLargest { |
703. Kth Largest Element in a Stream
https://xuanhe95.github.io/2022/04/09/703-Kth-Largest-Element-in-a-Stream/