-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaps:FindtheRunningMedian.java
More file actions
82 lines (69 loc) · 2.69 KB
/
Copy pathHeaps:FindtheRunningMedian.java
File metadata and controls
82 lines (69 loc) · 2.69 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.PriorityQueue;
public class Solution {
public static String printHeap(PriorityQueue<Integer> heap) {
return (Arrays.toString(heap.toArray()));
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
PriorityQueue<Integer> leftMaxHeap = new PriorityQueue<Integer>(n, Collections.reverseOrder());
PriorityQueue<Integer> rightMinHeap = new PriorityQueue<Integer>(n);
int[] a = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
for (int i = 0; i < n; i++) {
int val = a[i];
if (i == 0) {
rightMinHeap.add(val);
} else if (i == 1) {
if (val < rightMinHeap.peek()) {
leftMaxHeap.add(val);
} else {
leftMaxHeap.add(rightMinHeap.poll());
rightMinHeap.add(val);
}
} else {
if (val < leftMaxHeap.peek()) {
leftMaxHeap.add(val);
} else {
rightMinHeap.add(val);
}
}
// balance
if (leftMaxHeap.size() != rightMinHeap.size()) {
PriorityQueue<Integer> longHeap, shortHeap;
if (leftMaxHeap.size() > rightMinHeap.size()) {
longHeap = leftMaxHeap;
shortHeap = rightMinHeap;
} else {
longHeap = rightMinHeap;
shortHeap = leftMaxHeap;
}
if (longHeap.size() + leftMaxHeap.size() % 2 == 0) {
while(longHeap.size() > shortHeap.size()) {
shortHeap.add(longHeap.poll());
}
} else if (longHeap.size() - shortHeap.size() > 1) {
shortHeap.add(longHeap.poll());
}
}
if ((rightMinHeap.size() + leftMaxHeap.size()) % 2 == 0) {
System.out.println((rightMinHeap.peek() + leftMaxHeap.peek()) / 2.0);
} else {
PriorityQueue<Integer> longHeap;
if (leftMaxHeap.size() > rightMinHeap.size()) {
longHeap = leftMaxHeap;
} else {
longHeap = rightMinHeap;
}
System.out.println(longHeap.peek() * 1.0);
}
}
}
}