Easy
,Tree
,Heap
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.Example 1:
Constraints:
k
<= 104nums.length
<= 104nums[i]
<= 104val
<= 104add
.k
elements in the array when you search for the kth element.Yen-Chi ChenTue, May 23, 2023
Ron ChenTue, May 23, 2023
用sort也會過ㄏㄏ
MarsgoatTue, May 23, 2023
minHeap就不貼了,我直接讓chatGPT把我之前的maxHeap直接改寫成minHeap
MarsgoatTue, May 23, 2023
Jerry WuTrue, May23, 2023