## Sunday, January 8, 2017

### LeetCode 480 - Sliding Window Median

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:
`[2,3,4]` , the median is `3`
`[2,3]`, the median is `(2 + 3) / 2 = 2.5`
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Your job is to output the median array for each window in the original array.
For example,
Given nums = `[1,3,-1,-3,5,3,6,7]`, and k = 3.
```Window position                Median
---------------               -----
[1  3  -1] -3  5  3  6  7       1
1 [3  -1  -3] 5  3  6  7       -1
1  3 [-1  -3  5] 3  6  7       -1
1  3  -1 [-3  5  3] 6  7       3
1  3  -1  -3 [5  3  6] 7       5
1  3  -1  -3  5 [3  6  7]      6
```
Therefore, return the median sliding window as `[1,-1,-1,3,5,6]`.
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.

```维护二叉排序树hiSet与loSet，其中：

hiSet的最小元素 > loSet的最大元素

hiSet的大小 - loSet的大小 ∈ [0, 1]

若hiSet的大小 > loSet的大小，则返回hiSet.minValue

否则，返回(hiSet.minValue + loSet.maxValue) / 2

若hiSet为空，或者新元素 > hiSet.minValue，则加入hiSet

否则加入loSet

调整hiSet, loSet

若hiSet包含目标元素，则从hiSet中移除

否则，从loSet中移除

调整hiSet, loSet

若loSet的大小 > hiSet的大小，则将loSet的最大值弹出，加入hiSet

否则，若hiSet的大小 - loSet的大小 > 1，则将hiSet的最小值弹出，加入loSet```
private Comparator<Point> cmp = new Comparator<Point>(){ public int compare(Point a, Point b) { if (a.getX() != b.getX()) return Double.valueOf(b.getX()).compareTo(Double.valueOf(a.getX())); return Double.valueOf(b.getY()).compareTo(Double.valueOf(a.getY())); } }; private TreeSet<Point> hiSet = new TreeSet<>(cmp); private TreeSet<Point> loSet = new TreeSet<>(cmp); private double getMedian() { if (hiSet.size() > loSet.size()) { return hiSet.last().getX(); } return ((hiSet.last().getX()) + (loSet.first().getX())) / 2; } private void addValue(int val, int i) { Point p = new Point(val, i); if (hiSet.isEmpty() || hiSet.last().getX() < p.getX()) { hiSet.add(p); } else { loSet.add(p); } adjustSet(); } private void removeValue(int val, int i) { Point p = new Point(val, i); if (hiSet.contains(p)) { hiSet.remove(p); } else { loSet.remove(p); } adjustSet(); } private void adjustSet() { if (loSet.size() > hiSet.size()) { hiSet.add(loSet.pollFirst()); } else if (hiSet.size() > loSet.size() + 1) { loSet.add(hiSet.pollLast()); } } public double[] medianSlidingWindow(int[] nums, int k) { double[] ans = new double[nums.length - k + 1]; for (int i = 0; i < nums.length; i++) { if (i >= k) { removeValue(nums[i - k], i - k); } addValue(nums[i], i); if (i >= k - 1) { ans[i - k + 1] = getMedian(); } } return ans; }
https://discuss.leetcode.com/topic/74874/easy-to-understand-o-nlogk-java-solution-using-treemap
TreeMap is used to implement an ordered MultiSet.
In this problem, I use two Ordered MultiSets as Heaps. One heap maintains the lowest 1/2 of the elements, and the other heap maintains the higher 1/2 of elements.
This implementation is faster than the usual implementation that uses 2 PriorityQueues, because unlike PriorityQueue, TreeMap can remove arbitrary element in logarithmic time.

X.
https://discuss.leetcode.com/topic/74724/java-solution-using-two-priorityqueues
Almost the same idea of `Find Median from Data Stream` https://leetcode.com/problems/find-median-from-data-stream/
1. Use two `Heaps` to store numbers. `maxHeap` for numbers smaller than current median, `minHeap` for numbers bigger than and `equal` to current median. A small trick I used is always make size of `minHeap` equal (when there are `even` numbers) or 1 element more (when there are `odd` numbers) than the size of `maxHeap`. Then it will become very easy to calculate current median.
2. Keep adding number from the right side of the sliding window and remove number from left side of the sliding window. And keep adding current median to the result.
The documentation says `remove(Object)` takes linear time.
Then the overall runtime complexity is O(nk).
``````    PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(
new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i2.compareTo(i1);
}
}
);

public double[] medianSlidingWindow(int[] nums, int k) {
int n = nums.length - k + 1;
if (n <= 0) return new double[0];
double[] result = new double[n];

for (int i = 0; i <= nums.length; i++) {
if (i >= k) {
result[i - k] = getMedian();
remove(nums[i - k]);
}
if (i < nums.length) {
}
}

return result;
}

if (num < getMedian()) {
}
else {
}
if (maxHeap.size() > minHeap.size()) {
}
if (minHeap.size() - maxHeap.size() > 1) {
}
}

private void remove(int num) {
if (num < getMedian()) {
maxHeap.remove(num);
}
else {
minHeap.remove(num);
}
if (maxHeap.size() > minHeap.size()) {
}
if (minHeap.size() - maxHeap.size() > 1) {
}
}

private double getMedian() {
if (maxHeap.isEmpty() && minHeap.isEmpty()) return 0;

if (maxHeap.size() == minHeap.size()) {
return ((double)maxHeap.peek() + (double)minHeap.peek()) / 2.0;
}
else {
return (double)minHeap.peek();
}
}``````