Created
June 30, 2015 06:35
-
-
Save gabhi/af06adc3b5d464f84163 to your computer and use it in GitHub Desktop.
Median in a stream of integers (running integers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Similar to balancing BST in Method 2 above, we can use a max heap on left side to represent elements that are less than effective median, and a min heap on right side to represent elements that are greater than effective median. | |
After processing an incoming element, the number of elements in heaps differ utmost by 1 element. When both heaps contain same number of elements, we pick average of heaps root data as effective median. When the heaps are not balanced, we select effective median from the root of heap containing more elements. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment