-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContinuousMedian.java
40 lines (37 loc) · 1.08 KB
/
ContinuousMedian.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
import java.util.*;
// Do not edit the class below except for
// the insert method. Feel free to add new
// properties and methods to the class.
class Program {
static class ContinuousMedianHandler {
double median = 0;
private PriorityQueue<Integer> smaller = new PriorityQueue<>();
private PriorityQueue<Integer> larger = new PriorityQueue<>();
// O(log(n)) time | O(n) space
public void insert(int number) {
// Write your code here.
if (smaller.size() == 0 || number < -smaller.peek()) {
smaller.add(-number);
} else {
larger.add(number);
}
// rebalance heaps
if (smaller.size() - larger.size() == 2) {
larger.add(-smaller.poll());
} else if (larger.size() - smaller.size() == 2) {
smaller.add(-larger.poll());
}
// update median
if (smaller.size() == larger.size()) {
median = ((double) -smaller.peek() + (double) larger.peek()) / 2;
} else if (smaller.size() > larger.size()) {
median = -smaller.peek();
} else {
median = larger.peek();
}
}
public double getMedian() {
return median;
}
}
}