-
Notifications
You must be signed in to change notification settings - Fork 0
/
runningMedian.java
47 lines (39 loc) · 1.1 KB
/
runningMedian.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
//https://www.hackerrank.com/challenges/ctci-find-the-running-median
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(1,
Collections.reverseOrder());
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int temp;
while (n-- > 0) {
temp = in.nextInt();
maxHeap.add(temp);
if (maxHeap.size() > minHeap.size() + 1)
minHeap.add(maxHeap.poll());
if (minHeap.size() > 0 && maxHeap.size() > 0) {
int a = minHeap.peek();
int b = maxHeap.peek();
if (b > a) {
minHeap.add(maxHeap.poll());
maxHeap.add(minHeap.poll());
}
}
if (n % 2 == 0)
System.out.println(((float) maxHeap.peek() + minHeap.peek()) / 2);
else
System.out.println((float)maxHeap.peek());
}
}
}