forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunningMideanInStream.java
59 lines (55 loc) · 2.05 KB
/
RunningMideanInStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
The solution is using two heaps or two priority queues, one max the other min, and always balanced. By balanced, it means
the difference of the length of each data structure should be less than or equal to 1. And the min data structure should
store the highest half of the sorted list and the max structure needs to store the lowest of the sorted list. In this way
the insersion will take place in O(logn) and finding the median in O(1) as access time to top element of heap or priority queue is O(1).
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double median=0.0;
PriorityQueue<Double> minheap=new PriorityQueue<>();
/*
Comparator<Double> maxHeapComparator=new Comparator<Double>() {
@Override
public int compare(Double x, Double y) {
// TODO Auto-generated method stub
if(y>x)
return 1;
else if(y<x)
return -1;
else return 0;
}
};
*/
PriorityQueue<Double> maxheap=new PriorityQueue<>(Collections.reverseOrder());
for(int i=0;i<n;i++)
{
double num=sc.nextDouble();
if(num > median)
minheap.add(num);
else
maxheap.add(num);
int minsize=minheap.size();
int maxsize=maxheap.size();
if(Math.abs(minsize-maxsize) > 1)
{
boolean a=(minsize>maxsize) ? maxheap.add(minheap.remove()) : minheap.add(maxheap.remove()) ;
}
minsize=minheap.size();
maxsize=maxheap.size();
if(minsize == maxsize)
median = (minheap.peek() + maxheap.peek())/2;
else if(Math.abs(minsize-maxsize) == 1)
median = (minsize > maxsize) ? minheap.peek() : maxheap.peek() ;
System.out.println(median);
}
}
}