forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData Stream Median.java
More file actions
executable file
·76 lines (57 loc) · 2.2 KB
/
Data Stream Median.java
File metadata and controls
executable file
·76 lines (57 loc) · 2.2 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
H
把Input stream想成向上的山坡。山坡中间那点,自然就是median.
前半段,作为maxHeap,关注点是PriorityQueue的峰点,也就是实际上的median.
后半段,作为minHeap,正常的PriorityQueue。 开头是最小的。
Note:题目定义meadian = A[(n-1)/2],也就是说maxHeap需要和minHeap长度相等,或者多一个element,最后可以直接poll() and return.
```
/*
Numbers keep coming, return the median of numbers at every time a new number added.
Example
For numbers coming list: [1, 2, 3, 4, 5], return [1, 1, 2, 2, 3].
For numbers coming list: [4, 5, 1, 3, 2, 6, 0], return [4, 4, 4, 3, 3, 3, 3].
For numbers coming list: [2, 20, 100], return [2, 2, 20].
Challenge
Total run time in O(nlogn).
Clarification
What's the definition of Median? - Median is the number that in the middle of a sorted array.
If there are n numbers in a sorted array A, the median is A[(n - 1) / 2].
For example, if A=[1,2,3], median is 2. If A=[1,19], median is 1.
Tags Expand
LintCode Copyright Heap Priority Queue
*/
public class Solution {
/**
* @param nums: A list of integers.
* @return: the median of numbers
*/
public int[] medianII(int[] nums) {
int[] rst = new int[nums.length];
if (nums == null || nums.length == 0) {
return rst;
}
PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(10, new Comparator<Integer>() {
public int compare(Integer x, Integer y) {
return y - x;
}
});
rst[0] = nums[0];
maxHeap.offer(rst[0]);
for (int i = 1; i < rst.length; i++){
int preMedian = maxHeap.peek();
if (nums[i] > preMedian) {
minHeap.offer(nums[i]);
} else {
maxHeap.offer(nums[i]);
}
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.offer(maxHeap.poll());
} else if (maxHeap.size() < minHeap.size()) {
maxHeap.offer(minHeap.poll());
}
rst[i] = maxHeap.peek();
}
return rst;
}
}
```