Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated.
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.
For example,
[2,3,4], the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.
classMedianFinder:def__init__(self):""" initialize your data structure here. """self.h1=[]self.h2=[]defaddNum(self,num:int)->None:heappush(self.h1,num)heappush(self.h2,-heappop(self.h1))iflen(self.h2)-len(self.h1)>1:heappush(self.h1,-heappop(self.h2))deffindMedian(self)->float:iflen(self.h2)>len(self.h1):return-self.h2[0]return(self.h1[0]-self.h2[0])/2# Your MedianFinder object will be instantiated and called as such:# obj = MedianFinder()# obj.addNum(num)# param_2 = obj.findMedian()
classMedianFinder{privatePriorityQueue<Integer>q1=newPriorityQueue<>();privatePriorityQueue<Integer>q2=newPriorityQueue<>(Collections.reverseOrder());/** initialize your data structure here. */publicMedianFinder(){}publicvoidaddNum(intnum){q1.offer(num);q2.offer(q1.poll());if(q2.size()-q1.size()>1){q1.offer(q2.poll());}}publicdoublefindMedian(){if(q2.size()>q1.size()){returnq2.peek();}return(q1.peek()+q2.peek())*1.0/2;}}/** * Your MedianFinder object will be instantiated and called as such: * MedianFinder obj = new MedianFinder(); * obj.addNum(num); * double param_2 = obj.findMedian(); */
classMedianFinder{public:/** initialize your data structure here. */MedianFinder(){}voidaddNum(intnum){q1.push(num);q2.push(q1.top());q1.pop();if(q2.size()-q1.size()>1){q1.push(q2.top());q2.pop();}}doublefindMedian(){if(q2.size()>q1.size()){returnq2.top();}return(double)(q1.top()+q2.top())/2;}private:priority_queue<int,vector<int>,greater<int>>q1;priority_queue<int>q2;};/** * Your MedianFinder object will be instantiated and called as such: * MedianFinder* obj = new MedianFinder(); * obj->addNum(num); * double param_2 = obj->findMedian(); */
typeMedianFinderstruct{q1hpq2hp}/** initialize your data structure here. */funcConstructor()MedianFinder{returnMedianFinder{hp{},hp{}}}func(this*MedianFinder)AddNum(numint){heap.Push(&this.q1,num)heap.Push(&this.q2,-heap.Pop(&this.q1).(int))ifthis.q2.Len()-this.q1.Len()>1{heap.Push(&this.q1,-heap.Pop(&this.q2).(int))}}func(this*MedianFinder)FindMedian()float64{ifthis.q2.Len()>this.q1.Len(){return-float64(this.q2.IntSlice[0])}returnfloat64(this.q1.IntSlice[0]-this.q2.IntSlice[0])/2.0}/** * Your MedianFinder object will be instantiated and called as such: * obj := Constructor(); * obj.AddNum(num); * param_2 := obj.FindMedian(); */typehpstruct{sort.IntSlice}func(hhp)Less(i,jint)bool{returnh.IntSlice[i]<h.IntSlice[j]}func(h*hp)Push(vany){h.IntSlice=append(h.IntSlice,v.(int))}func(h*hp)Pop()any{a:=h.IntSlicev:=a[len(a)-1]h.IntSlice=a[:len(a)-1]returnv}