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

  • 相关阅读:
    2030
    2019
    2018
    在 《检验反相能力, 一题不会者 不配反相》 里 的 回复
    我转载了 历史吧 的 一个 帖 《为什么很多人说中国古代没有科学?这不扯淡嘛》 到 反相吧
    在 简单的数学题也不会做了。做一算术题,看看是否老年痴呆! 里 的 回复
    谈谈 光速
    相对论 的 时空观 本身 就会 导致 一个 绝对 的 参照系
    对 薛定谔 波函数, 我 关心 它的 推导依据, 不太关心 数学形式
    霍奇猜想 (二)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6378654.html
Copyright © 2011-2022 走看看