public class Solution {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>(){
@Override
public int compare(Integer o1,Integer o2){
return o2 - o1;
}
});
int count = 0;
//private ArrayList<Integer> data = new ArrayList<>();
public void Insert(Integer num) {
//data.add(num);
if(count % 2 == 0){
//已经偶数个 放到
maxHeap.offer(num);
int temp = maxHeap.poll();
minHeap.offer(temp);
}else{
minHeap.offer(num);
int temp = minHeap.poll();
maxHeap.offer(temp);
}
count ++;
}
public Double GetMedian() {
// Collections.sort(data);
// int length = data.size();
// if(length % 2 != 0){
// return (double)data.get(length / 2);
// }else{
// return (double)( data.get(length /2 - 1) +data.get(length / 2)) / 2;
// }
if(count % 2 == 0){
return new Double(minHeap.peek() + maxHeap.peek() ) / 2;
}else{
return new Double(minHeap.peek());
}
}
}