Repository navigation
Expand file tree
/
Copy path0480. Sliding Window Median.java
More file actions
62 lines (55 loc) · 2.06 KB
/
Copy path0480. Sliding Window Median.java
File metadata and controls
62 lines (55 loc) · 2.06 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
class Solution {
public double[] medianSlidingWindow(int[] nums, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
HashMap<Integer, Integer> map = new HashMap<>();
double[] result = new double[nums.length - k + 1];
int balance = 0; // Relative size: maxHeap.size() - minHeap.size()
for (int i = 0; i < nums.length; i++) {
// 1. ADD
if (maxHeap.isEmpty() || nums[i] <= maxHeap.peek()) {
maxHeap.offer(nums[i]);
balance++;
} else {
minHeap.offer(nums[i]);
balance--;
}
// 2. REMOVE
if (i >= k) {
int outNum = nums[i - k];
map.put(outNum, map.getOrDefault(outNum, 0) + 1);
if (outNum <= maxHeap.peek()) {
balance--;
} else {
balance++;
}
}
// 3. REBALANCE
if (balance > 1) {
minHeap.offer(maxHeap.poll());
balance -= 2;
} else if (balance < 0) {
maxHeap.offer(minHeap.poll());
balance += 2;
}
// 4. THE FIX: "Eager" Cleaning
while (!maxHeap.isEmpty() && map.getOrDefault(maxHeap.peek(), 0) > 0) {
int top = maxHeap.poll();
map.put(top, map.get(top) - 1);
}
while (!minHeap.isEmpty() && map.getOrDefault(minHeap.peek(), 0) > 0) {
int top = minHeap.poll();
map.put(top, map.get(top) - 1);
}
// 5. RESULT
if (i >= k - 1) {
if (k % 2 == 1) {
result[i - k + 1] = (double) maxHeap.peek();
} else {
result[i - k + 1] = ((double) maxHeap.peek() + minHeap.peek()) / 2.0;
}
}
}
return result;
}
}