forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
39 lines (37 loc) · 1.17 KB
/
Solution.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
class MedianFinder {
PriorityQueue<Integer> left;
PriorityQueue<Integer> right;
/** initialize your data structure here. */
public MedianFinder() {
this.left = new PriorityQueue<>(1, (x, y) -> -(x - y));
this.right = new PriorityQueue<>();
}
public void addNum(int num) {
if (this.left.size() == 0) {
this.left.offer(num);
return;
}
if (this.left.size() == this.right.size()) {
this.left.offer(num);
if (this.left.peek() > this.right.peek()) {
int t = this.right.poll();
this.right.offer(this.left.poll());
this.left.offer(t);
}
} else {
this.right.offer(num);
if (this.right.peek() < this.left.peek()) {
int t = this.right.poll();
this.right.offer(this.left.poll());
this.left.offer(t);
}
}
}
public double findMedian() {
if (this.left.size() == this.right.size()) {
return (this.left.peek() + this.right.peek()) * 0.5;
} else {
return this.left.peek() * 1.0;
}
}
}