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

  • 相关阅读:
    MySql存储过程—2、第一个MySql存储过程的建立
    MYSQL 存储过程1、SQL存储过程的基础知识
    [转]忍无可忍 献上一曲维族歌曲 《我们的客户是花园》(吐槽)
    由硬盘供电不稳、数据线品质差造成的蓝屏
    music Elton John
    一个人在艰苦中能雄起,那是能力和毅力,一个人在安逸中能雄起,那是自觉和自律。
    安装VS2012出问题后,反复重启电脑。
    山寨、低端、劣质的机箱前置面板的悲剧。
    SQL Server Management Studio (SSMS) 清除登录记录
    DataTable 分批处理,每批处理4行
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6378654.html
Copyright © 2011-2022 走看看