LeetCode——480. 滑动窗口中位数[Sliding Window Median]——分析及代码[Java]
一、题目
中位数是有序序列最中间的那个数。如果序列的长度是偶数,则没有最中间的数;此时中位数是最中间的两个数的平均数。
例如:
- [2,3,4],中位数是 3
- [2,3],中位数是 (2 + 3) / 2 = 2.5
给你一个数组 nums,有一个长度为 k 的窗口从最左端滑动到最右端。窗口中有 k 个数,每次窗口向右移动 1 位。你的任务是找出每次窗口移动后得到的新窗口中元素的中位数,并输出由它们组成的数组。
示例:
给出 nums = [1,3,-1,-3,5,3,6,7],以及 k = 3。
窗口位置 中位数
--------------- -----
[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
因此,返回该滑动窗口的中位数数组 [1,-1,-1,3,5,6]。
提示:
- 你可以假设 k 始终有效,即:k 始终小于输入的非空数组的元素个数。
- 与真实值误差在 10 ^ -5 以内的答案将被视作正确答案。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sliding-window-median
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
二、分析及代码
1. 双优先队列 + 延迟删除
(1)思路
设计两个堆,用大顶堆存储窗口中较小的一半数据,小顶堆存储窗口中较大的一半数据,并在窗口滑动过程中实时维护。
由于堆中无法快速删除任意元素,设计一个哈希表存储待删除的元素,当这些元素移动到堆顶时再将其从堆中删除。
(2)代码
class Solution {
public double[] medianSlidingWindow(int[] nums, int k) {
DualHeap dualHeap = new DualHeap();
int len = nums.length;
double[] ans = new double[len - k + 1];
for (int i = 0; i < k; i++)
dualHeap.insert(nums[i]);
ans[0] = dualHeap.getMedian();
for (int i = k; i < len; i++) {
dualHeap.insert(nums[i]);
dualHeap.del(nums[i - k]);
ans[i - k + 1] = dualHeap.getMedian();
}
return ans;
}
}
//双优先队列 + 延迟删除
class DualHeap {
private PriorityQueue<Integer> smallNums;//大顶堆,记录小数
private PriorityQueue<Integer> largeNums;//小顶堆,记录大数
private int smallSize, largeSize;//小、大数实际个数,滑动窗口大小
private Map<Integer, Integer> toDel;
public DualHeap() {
this.smallNums = new PriorityQueue<Integer>(new Comparator<Integer>() {
public int compare(Integer num1, Integer num2) {
return num2.compareTo(num1);
}
});
this.largeNums = new PriorityQueue<Integer>(new Comparator<Integer>() {
public int compare(Integer num1, Integer num2) {
return num1.compareTo(num2);
}
});
this.smallSize = 0;
this.largeSize = 0;
this.toDel = new HashMap<Integer, Integer>();
}
public double getMedian() {
return (smallSize > largeSize) ? smallNums.peek() : ((double)smallNums.peek() + largeNums.peek()) / 2;
}
public void insert(int num) {
if (smallNums.isEmpty() || num <= smallNums.peek()) {
smallNums.offer(num);
smallSize++;
} else {
largeNums.offer(num);
largeSize++;
}
balance();
return;
}
public void del(int num) {
toDel.put(num, toDel.getOrDefault(num, 0) + 1);
if (num <= smallNums.peek()) {
smallSize--;
prune(smallNums);
} else {
largeSize--;
prune(largeNums);
}
balance();
}
//弹出堆顶待删除的元素,更新哈希表
public void prune(PriorityQueue<Integer> heap) {
while (!heap.isEmpty()) {
int num = heap.peek();
if (toDel.containsKey(num)) {
//堆顶待删除
toDel.put(num, toDel.get(num) - 1);
if (toDel.get(num) == 0)
toDel.remove(num);
heap.poll();
} else {
return;
}
}
}
//保证小数与大数数量相同或多1
public void balance() {
if (smallSize > largeSize + 1) {
largeNums.offer(smallNums.poll());
smallSize--;
largeSize++;
prune(smallNums);
} else if (smallSize < largeSize) {
smallNums.offer(largeNums.poll());
smallSize++;
largeSize--;
prune(largeNums);
}
return;
}
}
(3)结果
执行用时 :21 ms,在所有 Java 提交中击败了 97.91% 的用户;
内存消耗 :40.6 MB,在所有 Java 提交中击败了 49.65% 的用户。
三、其他
暂无。