1. 程式人生 > >703. Kth Largest Element in a Stream

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.

Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return
the element representing the kth largest element in the stream. Example: int k = 3; int[] arr = [4,5,8,2]; KthLargest kthLargest = new KthLargest(3, arr); kthLargest.add(3); // returns 4 kthLargest.add(5); // returns 5 kthLargest.add(10); // returns 5 kthLargest.add(9); // returns 8 kthLargest.add(4); //
returns 8 We can build a MinHeap that contains only k largest elements.
On add: * compare a new element x with min to decide if we should pop min and insert x * take into account a case when heap_size is less than k Construction is simply calling the add function N times. Time complexity:
* Construction: O(N * logK) * Adding: O(logK) Additional memory: * O(K) (can be reduced to O(1) by reusing memory of the existing array) class KthLargest { PriorityQueue<Integer> pq; int k; public KthLargest(int k, int[] nums) { pq = new PriorityQueue<>(); this.k = k; for(int num : nums){ pq.offer(num); if(pq.size() > k) pq.poll(); } } public int add(int val) { pq.offer(val); if(pq.size() > k) pq.poll(); return pq.peek(); } }