Find the median of the running stream. Median is the middle element when we sort them.
Example:
Input: arr[]={2,3,4}
Output: 2 2.5 3
Approach:
Java
import java.util.PriorityQueue;public class MedianFinderStream {public static void main(String[] args) {MedianFinder m = new MedianFinder();m.addNum(2);System.out.println(m.findMedian());m.addNum(3);System.out.println(m.findMedian());m.addNum(4);System.out.println(m.findMedian());}}class MedianFinder {// maximum priority queuePriorityQueue<Integer> max =new PriorityQueue<>((a, b) -> b - a);// minimum priority queuePriorityQueue<Integer> min = new PriorityQueue<>();// variable to hold the mediandouble median = 0;/** initialize your data structure here. */public MedianFinder() {}public void addNum(int num) {// if both are of equal sizeif (max.size() == min.size()) {// if current element is// greater than median then// push into minimum priority// queue and set median as top// of min priority queueif (num > median) {min.add(num);median = min.peek();}// else push into the max queue// and set median as top of max queueelse {max.add(num);median = max.peek();}}// if min size is moreelse if (min.size() > max.size()) {// if element is more than median// then push the top of min into max// and current element into the minif (num > median) {max.add(min.poll());min.add(num);}// else push into the maxelsemax.add(num);// set median as average of// min top and max top elementsmedian = (double) (min.peek() + max.peek()) / 2;}// if max size if greaterelse {// if current is more than then// median then push into the min// queueif (num > median) {min.add(num);}// else push the top of max into// the min and current into the maxelse {min.add(max.poll());max.add(num);}// set the median as average of// top of min and maxmedian = (double) (min.peek() + max.peek()) / 2;}}public double findMedian() {return median;}}
C++
#include <bits/stdc++.h>using namespace std;//maximum priority queuepriority_queue<double> max1;//minimum priority queuepriority_queue<double,vector<double>,greater<double>> min1;//variable to hold the mediandouble median=0;//add numner into the priority//queuesvoid addNum(int num){//if both are of equal sizeif(max1.size()==min1.size()){//if current element is//greter than median then//push into minimum priority//queue and set meadin as top//of min priority queueif(num>median){min1.push(num);median=min1.top();}//else push into the max queue//and set median as top of max queueelse{max1.push(num);median=max1.top();}}//if min size is moreelse if(min1.size()>max1.size()){//if element is more than median//then push the top of min into max//and current element into the minif(num>median){max1.push(min1.top());min1.pop();min1.push(num);}//else push into the maxelsemax1.push(num);//set median as average of//min top and max top elementsmedian=(min1.top()+max1.top())/2;}//if max size if greaterelse{//if current is more than then//median then push into the min//queueif(num>median){min1.push(num);}//else push the top of max into//the min and current into the maxelse{min1.push(max1.top());max1.pop();max1.push(num);}//set the median as average of//top of min and maxmedian=(min1.top()+max1.top())/2;}}//function to find the meadin of running//streamdouble findMedian() {return median;}int main(){int arr[]={2,3,4};int n=sizeof(arr)/sizeof(arr[0]);for(int i=0;i<n;i++){addNum(arr[i]);cout<<findMedian()<<" ";}return 0;}
No comments:
Post a Comment