zoukankan      html  css  js  c++  java
  • 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.

    题解:

    使用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(nk), n = nums.length. 对于minHeap 和 maxHeap来说每个元素add, remove O(1)次. remove(target) takes O(k).

    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 }

    类似Find Median from Data Stream

  • 相关阅读:
    Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
    Codeforces Round #333 (Div. 2) A. Two Bases 水题
    SPOJ 1557. Can you answer these queries II 线段树
    线段树 模板
    Codeforces Round #115 B. Plane of Tanks: Pro 水题
    Codeforces Round #115 A. Robot Bicorn Attack 暴力
    Codeforces Beta Round #51 C. Pie or die 博弈论找规律 有趣的题~
    Codeforces Beta Round #51 B. Smallest number dfs
    Codeforces Beta Round #51 A. Flea travel 水题
    Codeforces Beta Round #51 D. Beautiful numbers 数位dp
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6378654.html
Copyright © 2011-2022 走看看