Numbers keep coming, return the median of numbers at every time a new number added.
For numbers coming list: [1, 2, 3, 4, 5], return [1, 1, 2, 2, 3]
For numbers coming list: [4, 5, 1, 3, 2, 6, 0], return [4, 4, 4, 3, 3, 3, 3]
For numbers coming list: [2, 20, 100], return [2, 2, 20]
O(nlogn) time
Clarification
What's the definition of Median?
- Median is the number that in the middle of a sorted array. If there are n numbers in a sorted array A, the median is A[(n-1)/2].
- For example, if A=[1,2,3], median is 2. If A=[1,19], median is 1
Analysis:
We maintain a max heap and a min heap. At any index i, the max heap stores the elements small or equal than the current median and the min heap stores the elements that are larger than the current median. Because in the problem, we select the median as at the position (n-1)/2, so we should always keep the size of max heap equal or one larger than the min heap. At odd index, we try to rebalance the size of max heap and min heap, while at even index, we try to make the size of max heap one larger than that of the min heap. By doing this, at each position, after inserting A[i] into the heaps, the root value of the max heap is the median.
NOTE: if we select (n-1)/2+1 as median, we can then keep the min heap equal or one larger than the max heap, the root value of the min heap is the median.
Solution:
I implement a Heap class with only insert and popHeapRoot functions.
1 class Heap{ 2 private int[] nodes; 3 private int size; 4 private boolean isMaxHeap; 5 6 public Heap(int capa, boolean isMax){ 7 nodes = new int[capa]; 8 size = 0; 9 isMaxHeap = isMax; 10 } 11 12 public boolean isEmpty(){ 13 if (size==0) return true; 14 else return false; 15 } 16 17 public int getHeapRootValue(){ 18 //should throw exception when size==0; 19 return nodes[0]; 20 } 21 22 private void swap(int x, int y){ 23 int temp = nodes[x]; 24 nodes[x] = nodes[y]; 25 nodes[y] = temp; 26 } 27 28 public boolean insert(int val){ 29 if (size==nodes.length) return false; 30 size++; 31 nodes[size-1]=val; 32 //check its father iteratively. 33 int cur = size-1; 34 int father = (cur-1)/2; 35 while (father>=0 && ((isMaxHeap && nodes[cur]>nodes[father]) || (!isMaxHeap && nodes[cur]<nodes[father]))){ 36 swap(cur,father); 37 cur = father; 38 father = (cur-1)/2; 39 } 40 return true; 41 } 42 43 private void shiftDown(int ind){ 44 int left = (ind+1)*2-1; 45 int right = (ind+1)*2; 46 while (left<size || right<size){ 47 if (isMaxHeap){ 48 int leftVal = (left<size) ? nodes[left] : Integer.MIN_VALUE; 49 int rightVal = (right<size) ? nodes[right] : Integer.MIN_VALUE; 50 int next = (leftVal>=rightVal) ? left : right; 51 if (nodes[ind]>nodes[next]) break; 52 else { 53 swap(ind,next); 54 ind = next; 55 left = (ind+1)*2-1; 56 right = (ind+1)*2; 57 } 58 } else { 59 int leftVal = (left<size) ? nodes[left] : Integer.MAX_VALUE; 60 int rightVal = (right<size) ? nodes[right] : Integer.MAX_VALUE; 61 int next = (leftVal<=rightVal) ? left : right; 62 if (nodes[ind]<nodes[next]) break; 63 else { 64 swap(ind,next); 65 ind = next; 66 left = (ind+1)*2-1; 67 right = (ind+1)*2; 68 } 69 } 70 } 71 } 72 73 public int popHeapRoot(){ 74 //should throw exception, when heap is empty. 75 76 int rootVal = nodes[0]; 77 swap(0,size-1); 78 size--; 79 if (size>0) shiftDown(0); 80 return rootVal; 81 } 82 } 83 84 85 86 87 public class Solution { 88 /** 89 * @param nums: A list of integers. 90 * @return: the median of numbers 91 */ 92 public int[] medianII(int[] nums) { 93 int[] res = new int[nums.length]; 94 Heap maxHeap = new Heap(nums.length,true); 95 Heap minHeap = new Heap(nums.length,false); 96 maxHeap.insert(nums[0]); 97 res[0] = nums[0]; 98 for (int i=1;i<nums.length;i++) 99 if (i %2 == 1) { //i is odd index. 100 int median = maxHeap.getHeapRootValue(); 101 if (nums[i]<median){ 102 maxHeap.popHeapRoot(); 103 minHeap.insert(median); 104 maxHeap.insert(nums[i]); 105 res[i] = maxHeap.getHeapRootValue(); 106 } else { 107 res[i] = median; 108 minHeap.insert(nums[i]); 109 } 110 } else { //i is even index. 111 int median = maxHeap.getHeapRootValue(); 112 if (nums[i]<median){ 113 maxHeap.insert(nums[i]); 114 } else { 115 minHeap.insert(nums[i]); 116 int val = minHeap.popHeapRoot(); 117 maxHeap.insert(val); 118 } 119 res[i] = maxHeap.getHeapRootValue(); 120 } 121 122 return res; 123 } 124 }