Problem: An odd number K
is given. At the input, we gradually receive a sequence of N
numbers. After reading each number (except the first K-1
), print the median of the last K
numbers.
I solved it using two heaps (MaxHeap and MinHeap, e.g. like here ) and added function remove_element(value)
to both heaps. remove_element(value)
iterates over the heap, looking for the value to remove. Removes it and then rebalances the tree. So it works in O(K+log K)
.
And I solved the problem like this: by iterating the stream date, I add a new element to the heaps and delete the old one, which is already outside the window (K+1
th element). So it works in O(N*(K + log K + log K)) = O(N*K)
.
I'm wondering 1) if I correctly estimated the time complexity of the algorithm, 2) is it possible to speed up this algorithm. 3) if not (the algorithm is optimal), then how can I prove its optimality.
As for the third question, if my estimate of the O(N*K) algorithm is correct, I think it can be proven based on the obvious idea that in order to write out the median, we need to check all K elements for each of N requests