LeetCode 480. Sliding Window Median

原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description

题目:

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.

题解:

类似Find Median from Data Stream.

使用minHeap和maxHeap来维护median. 为了方便,这里始终保持minHeap.size() == maxHeap.size() 或 minHeap.size() = maxHeap.size()+1.

所以最开始两个heap都为空时,加到minHeap中. 

取median时若size相同就两边peek求和除以2. 若size不同,那么肯定minHeap size大, minHeap peek下就是median.

remove时,看要remove的数nums[i-k], 若比median小,从maxHeap中remove. 不然从minHeap中remove.

Note: 两遍peek求和时注意overflow.

Remove时如果出现小数, 多半会从小的这一侧remove, 也就是maxHeap中remove. 所以添加时应该尽量向大的这一侧添加. 但添加时检测要用!maxHeap.isEmpty()&&maxHeap.peek()>nums[i]限制小的一侧添加, 而不用minHeap.isEmpty() || minHeap.peek()<nums[i]胡乱往大的一侧添加. 因为有可能minHeap刚才经过remove已经空了, 若出现个很小的数就错误的加进了minHeap中.

Time Complexity: O(nlogk), n = nums.length. 对于minHeap 和 maxHeap来说每个元素add, remove O(1)次.

Space: O(k).

AC java:

 1 public class Solution {
 2     public double[] medianSlidingWindow(int[] nums, int k) {
 3         if(nums == null || nums.length == 0 || k <= 0){
 4             return new double[0];
 5         }
 6         
 7         PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
 8         PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
 9         
10         int len = nums.length;
11         double [] res = new double[len-k+1];
12         for(int i = 0; i<=len; i++){
13             if(i>=k){
14                 if(minHeap.size() == maxHeap.size()){
15                     res[i-k] = ((double)minHeap.peek() + (double)maxHeap.peek())/2.0;
16                 }else{
17                     res[i-k] = minHeap.peek();
18                 }
19                 if(nums[i-k] < res[i-k]){
20                     maxHeap.remove(nums[i-k]);
21                 }else{
22                     minHeap.remove(nums[i-k]);
23                 }
24             }
25             if(i<len){
26                 if(!maxHeap.isEmpty() && maxHeap.peek()>nums[i]){
27                     maxHeap.offer(nums[i]);
28                 }else{
29                     minHeap.offer(nums[i]);
30                 }
31                 while(maxHeap.size() > minHeap.size()){
32                     minHeap.offer(maxHeap.poll());
33                 }
34                 while(minHeap.size() - maxHeap.size() > 1){
35                     maxHeap.offer(minHeap.poll());
36                 }
37             }
38         }
39         return res;
40     }
41 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6378654.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值