703 Kth Largest Element in a Stream
1. Question
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.
2. Implementation
思路: 用Heap解决数据流第K大的问题
3. Time & Space Complexity
Heap: 时间复杂度O(nlogk), 空间复杂度O(k)
Last updated
Was this helpful?