703 Kth Largest Element in a Stream
Design a class to find thekth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Your KthLargest
class will have a constructor which accepts an integerk
and an integer arraynums
, which contains initial elements from the stream. For each call to the methodKthLargest.add
, return the element representing the kth largest element in the stream.
Example:
Note:
You may assume that nums
' length ≥ k-1
andk
≥ 1.
这题好像是真题,在面FB时就被当作follow up,估计是我脑残一上来就给quick select。嘛,这题其实比较简单。用的是heap,因为我们求largest,所以用min heap。
Last updated