Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:[2,3,4] , the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Your job is to output the median array for each window in the original array.
For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
Window position Median --------------- ----- [1 3 -1] -3 5 3 6 7 1 1 [3 -1 -3] 5 3 6 7 -1 1 3 [-1 -3 5] 3 6 7 -1 1 3 -1 [-3 5 3] 6 7 3 1 3 -1 -3 [5 3 6] 7 5 1 3 -1 -3 5 [3 6 7] 6
Therefore, return the median sliding window as [1,-1,-1,3,5,6].
Note:
You may assume k is always valid, ie: k is always smaller than input array's size for non-empty array.
This problem is a companion problem to 295. Find Median From Data Stream. This means that a lot of approaches to solve this problem are based on the methods to solve 295. Find Median From Data Stream. Perhaps try that problem before you approach this one.
\nIntuition
\nDo what the question says.
\nAlgorithm
\nStore the numbers in a window container of size . The following operations must take place:
\nOne primitive approach is to copy consecutive elements from the input to the window and keep sorting these every time. This constitutes duplication of effort.
\nWe can do a bit better if we instead insert and delete one element per window shift. The challenge then is to maintain the window as sorted, before and after the insert and delete operations.
\nC++ [Time Limit Exceeded]
\n\nPython [Accepted]
\nPython comes with an excellent bisect module to help perform efficient insert operations on lists while maintaining their sorted property.
Complexity Analysis
\nTime complexity: to .
\nSorting for each of the sliding window instances takes about time each.
\nBisected insertion or deletion takes about for searching and for actual shifting of elements. This takes place about times.
\nSpace complexity: extra linear space for the window container.
\nIntuition
\nThe idea is the same as Approach #3 from 295. Find Median From Data Stream. The only additional requirement is removing the outgoing elements from the window.
\nSince the window elements are stored in heaps, deleting elements that are not at the top of the heaps is a pain.
\nSome languages (like Java) provide implementations of the PriorityQueue class that allow for removing arbitrarily placed elements. Generally, using such features is not efficient nor is their portability assured.
Assuming that only the tops of heaps (and by extension the PriorityQueue class) are accessible, we need to find a way to efficiently invalidate and remove elements that are moving out of the sliding window.
At this point, an important thing to notice is the fact that if the two heaps are balanced, only the top of the heaps are actually needed to find the medians. This means that as long as we can somehow keep the heaps balanced, we could also keep some extraneous elements.
\nThus, we can use hash-tables to keep track of invalidated elements. Once they reach the heap tops, we remove them from the heaps. This is the lazy removal technique.
\nAn immediate challenge at this point is balancing the heaps while keeping extraneous elements. This is done by actually moving some elements to the heap which has extraneous elements, from the other heap. This cancels out the effect of having extraneous elements and maintains the invariant that the heaps are balanced.
\nNOTE: When we talk about keeping the heaps balanced, we are not referring to the actual heap sizes. We are only concerned with valid elements and hence when we talk about balancing heaps, we are referring to count of such elements.
\nAlgorithm
\nTwo priority queues:
\nlo to store the smaller half of the numbershi to store the larger half of the numbersA hash-map or hash-table hash_table for keeping track of invalid numbers. It holds the count of the occurrences of all such numbers that have been invalidated and yet remain in the heaps.
The max-heap lo is allowed to store, at worst, one more element more than the min-heap hi. Hence if we have processed elements:
lo is allowed to hold elements, while hi can hold elements.This gives us the nice property that when the heaps are perfectly balanced, the median can be derived from the tops of both heaps. Otherwise, the top of the max-heap lo holds the legitimate median.
NOTE: As mentioned before, when we are talking about keeping the heaps balanced, the actual sizes of the heaps are irrelevant. Only the count of valid elements in both heaps matter.
\nKeep a balance factor. It indicates three situations:
balance : Both heaps are balanced or nearly balanced.balance : lo needs more valid elements. Elements from hi are moved to lo.balance : hi needs more valid elements. Elements from lo are moved to hi.Inserting an incoming number in_num:
If in_num is less than or equal to the top element of lo, then it can be inserted to lo. However this unbalances hi (hi has lesser valid elements now). Hence balance is incremented.
Otherwise, in_num must be added to hi. Obviously, now lo is unbalanced. Hence balance is decremented.
Lazy removal of an outgoing number out_num:
out_num is present in lo, then invalidating this occurrence will unbalance lo itself. Hence balance must be decremented.If out_num is present in hi, then invalidating this occurrence will unbalance hi itself. Hence balance must be incremented.
We increment the count of this element in the hash_table table.
\nComplexity Analysis
\nTime complexity: .
\nSpace complexity: extra linear space.
\nIntuition
\nOne can see that multisets are a great way to keep elements sorted while providing efficient access to the first and last elements. Inserting and deleting arbitrary elements are also fairly efficient operations in a multiset. (Refer to Approach #4 Intuition for 295. Find Median From Data Stream)
Thus, if the previous approach gives you too much heartburn, consider replacing the use of priority_queue with multiset.
Algorithm
\nInserting or deleting an element is straight-forward. Balancing the heaps takes the same route as Approach #3 of 295. Find Median From Data Stream.
\n\nComplexity Analysis
\nTime complexity: .
\nSpace complexity: extra linear space to hold contents of the window.
\nIntuition
\nThis is same as Approach #4 for 295. Find Median From Data Stream.
\n\n\nBut, we don\'t actually need two pointers.
\n
Median elements are derived using a single iterator position (when the window size is odd) or two consecutive iterator positions (when is even). Hence keeping track of only one pointer is sufficient. The other pointer can be implicitly derived when required.
\nAlgorithm
\nA single iterator mid, which iterates over the window multiset. It is analogous to upper_median in Approach #4 for 295. Find Median From Data Stream. lower_median is implicitly derived from mid. It\'s either equal to mid (when the window size is odd) or prev(mid) 1 .
We start with populating our multiset window with the first elements. We set mid to the indexed element in window (0-based indexing; Multisets always maintain their sorted property).
While inserting an element num into window, three cases arise:
num is less than the value of upper median mid.
num is greater than the value of upper median mid.
num is equal to the value of upper median mid. This situation is often handled as language-dependent. Since C++ multiset insert elements at the end of their equal range, this situation is essentially the same as the previous case.
For the first case, num is inserted before the upper median element mid. Thus mid now, no longer points to the indexed element. In fact it points to the indexed element. We fix that by decrementing mid.
For the second and third cases, num is inserted after the upper median element mid and hence does not spoil the mid iterator. It still points to the indexed element.
Of course, the window size just increased to in all three cases. That will easily be fixed by removing the element that is about to exit the window.
\nWhile removing an element num, the same three cases arise as when we wanted to insert an element:
num is less than the value of upper median mid.
num is greater than the value of upper median mid.
num is equal to the value of upper median mid. Since mid will point to the first occurrence of num in the multiset window and we deterministically remove the first occurrence (take note that we use std::multiset::lower_bound() 2 to find the correct occurrence to erase), this case is handled in the same manner as the first case.
In the first and third cases, removing num will spoil the mid iterator. Thus we need to fix that by incrementing mid before we remove that element.
For the second case, the mid iterator is not spoiled. No further action is required.
Once this element has been removed, the window size returns to being .
\nAfter insertion of the incoming element and removal of the outgoing element, we are left again with some nice invariants:
\nmid still points to the indexed element.Finding the median of the window is easy! It is simply the mean of the elements pointed to by the two pointers lo_median and hi_median. In our case those are mid or prev(mid) (as decided by whether is odd or even) , and mid respectively.
Complexity Analysis
\nTime complexity: .
\nmid takes about time.multiset scheme. 4mid iterator.Space complexity: extra linear space to hold contents of the window.
\nAs noted before, this problem is essentially an extension to 295. Find Median From Data Stream. That problem had a lot of ways to go about, that frankly, are not of much use in an interview. But they are interesting to follow all the same. If you are interested take a look here. Try extending those methods to this problem.
\nAnalysis written by @babhishek21.
\nstd::prev() is a C++ method to find the previous element to the current one being pointed to by an iterator.\xc2\xa0\xe2\x86\xa9
Had we used std::multiset::find(), there was no guarantee that the first occurrence of num would be found. Although the contrary did happen in all of our tests, I don\'t recommend using it. Your mileage may vary.\xc2\xa0\xe2\x86\xa9
Shout-out to @votrubac and @StefanPochmann!\xc2\xa0\xe2\x86\xa9
\nHinting can reduce that to amortized constant time.\xc2\xa0\xe2\x86\xa9
\n