http://www.geeksforgeeks.org/kth-largest-element-in-a-stream
Given an infinite stream of integers, find the k’th largest element at any point of time.
Example:
Input: stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...} k = 3 Output: {_, _, 10, 11, 20, 40, 50, 50, ...}
Extra space allowed is O(k).
X. PriorityQueue
https://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/149050/Java-Priority-Queue
https://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/149050/Java-Priority-Queue
Keep track of the k biggest elements in the minimum priority queue
q
. q.peek()
is the answer. final PriorityQueue<Integer> q;
final int k;
public KthLargest(int k, int[] a) {
this.k = k;
q = new PriorityQueue<>(k);
for (int n : a)
add(n);
}
public int add(int n) {
if (q.size() < k)
q.offer(n);
else if (q.peek() < n) {
q.poll();
q.offer(n);
}
return q.peek();
}
https://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/150400/MinHeap-solution PriorityQueue<Integer> minHeap;
int k;
public KthLargest(int k, int[] nums) {
minHeap = new PriorityQueue<>();
this.k = k;
for(int val: nums) {
if(minHeap.size()<k||val>minHeap.peek()) minHeap.add(val);
if(minHeap.size()>k) minHeap.poll();
}
}
public int add(int val) {
if(minHeap.size()<k||val>minHeap.peek()) minHeap.add(val);
if(minHeap.size()>k) minHeap.poll();
return minHeap.peek();
}
void
kthLargest(
int
k)
{
// count is total no. of elements in stream seen so far
int
count = 0, x;
// x is for new element
// Create a min heap of size k
int
*arr =
new
int
[k];
MinHeap mh(arr, k);
while
(1)
{
// Take next element from stream
cout <<
"Enter next element of stream "
;
cin >> x;
// Nothing much to do for first k-1 elements
if
(count < k-1)
{
arr[count] = x;
count++;
}
else
{
// If this is k'th element, then store it
// and build the heap created above
if
(count == k-1)
{
arr[count] = x;
mh.buildHeap();
}
// If next element is greater than k'th
// largest, then replace the root
if
(x > mh.getMin())
mh.replaceMin(x);
// replaceMin calls heapify()
// Root of heap is k'th largest element
cout <<
"K'th largest element is "
<< mh.getMin() << endl;
count++;
}
}
}
http://prpds.blogspot.com/2011/07/kth-largest-from-infinite-stream.html
X. Binary Search Tree
https://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/147729/O(h)-Java-Solution-Using-BST
Extend:
https://discuss.leetcode.com/topic/115/uber-find-the-largest-kth-element-in-a-data-stream
Suppose you have a system, the input is an dynamic integer data stream. Suppose the data stream contains duplicate value. Find the kth largest elements in the data stream and return the index. If the duplicate values exist in multiple index, return them all.
Eg: The data stream is 4,2,2,3,1, k is 4, the answer is 2. The index is 1,2.
The data steam is 1,2,3,4,5, k is 3, the answer is 3 and the index is 2.
We can use MinHeap + hashatable, minheap to maintain k largest element, use hashtable to store the count. when element is removed from heap, it's also removed from the hashtable. Space is O(K).
Still remember some genius' code was to store pair<int value, int idx> in a heap, then keep drawing them out. reply
X. Binary Search Tree
https://leetcode.com/problems/kth-largest-element-in-a-stream/discuss/147729/O(h)-Java-Solution-Using-BST
h = height of tree with the average and best time O(log n) and worst time O(n)
Constructor O(nh)
Add O(h)
findKthLargest O(h)
Add O(h)
findKthLargest O(h)
Please read this for explanation.
TreeNode root;
int k;
public KthLargest(int k, int[] nums) {
this.k = k;
for (int num: nums) root = add(root, num);
}
public int add(int val) {
root = add(root, val);
return findKthLargest();
}
private TreeNode add(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
root.count++;
if (val < root.val) root.left = add(root.left, val);
else root.right = add(root.right, val);
return root;
}
public int findKthLargest() {
int count = k;
TreeNode walker = root;
while (count > 0) {
int pos = 1 + (walker.right != null ? walker.right.count : 0);
if (count == pos) break;
if (count > pos) {
count -= pos;
walker = walker.left;
} else if (count < pos)
walker = walker.right;
}
return walker.val;
}
class TreeNode {
int val, count = 1;
TreeNode left, right;
TreeNode(int v) { val = v; }
}
Extend:
https://discuss.leetcode.com/topic/115/uber-find-the-largest-kth-element-in-a-data-stream
Suppose you have a system, the input is an dynamic integer data stream. Suppose the data stream contains duplicate value. Find the kth largest elements in the data stream and return the index. If the duplicate values exist in multiple index, return them all.
Eg: The data stream is 4,2,2,3,1, k is 4, the answer is 2. The index is 1,2.
The data steam is 1,2,3,4,5, k is 3, the answer is 3 and the index is 2.
We can use MinHeap + hashatable, minheap to maintain k largest element, use hashtable to store the count. when element is removed from heap, it's also removed from the hashtable. Space is O(K).
Still remember some genius' code was to store pair<int value, int idx> in a heap, then keep drawing them out. reply