forked from shogunsea/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian-ii.java
71 lines (65 loc) · 2.52 KB
/
median-ii.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class Solution {
/**
* @param nums: A list of integers.
* @return: the median of numbers
*/
public int[] medianII(int[] nums) {
// write your code here
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(10,
new Comparator<Integer>(){
public int compare(Integer a, Integer b) {
return a - b;
}
});
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(10,
new Comparator<Integer>(){
public int compare(Integer a, Integer b) {
return b - a;
}
});
int len = nums.length;
int[] res = new int[len];
res[0] = nums[0];
minHeap.add(nums[0]);
for (int i = 1; i < len; i++) {
// First compare current number with minHeap
// then add it two min/maxHeap accordingly.
// Then banlance two heaps so that they only differ
// by at most one element.
// Last compare the size and get the median: if same size
// then median is average of two roots. Otherwise median
// is the root of longer heap.
// push to heaps.
// minHeap: store the bigger value. root(min) value is the smallest
// element in right half of the array.
// maxHeap: store smaller values. root(max) value is the largest element
// in the left half of the array.
int current = nums[i];
// {4, 5, 1, 3, 2, 6, 0};
// 4
// 5
if (current < minHeap.peek()) {
maxHeap.add(current);
} else {
minHeap.add(current);
}
// balance the heaps.
if (minHeap.size() > maxHeap.size() + 1) {
maxHeap.add(minHeap.poll());
} else if (maxHeap.size() > minHeap.size() + 1) {
minHeap.add(maxHeap.poll());
}
int minSize = minHeap.size();
int maxSize = maxHeap.size();
if (minSize == maxSize) {
// based on the median definition, when length is even
// just return the left middle.
// otherwise do an average of left and right.
res[i] = maxHeap.peek();
} else {
res[i] = minSize > maxSize? minHeap.peek() : maxHeap.peek();
}
}
return res;
}
}