Leetcode 480. Sliding Window Median 滑動視窗中的中位元 解題報告

來源:互聯網
上載者:User
1 解題思想

題目會給一個數組,和一個滑動視窗的大小K,讓你找出當這個視窗滑動的過程中,這個K的視窗內的中位元分別是多少。

最naive的方式就是在k個視窗內排序就好,這裡不解釋(因為開銷很大啊,(n-k+1) * (k*log(k))。。

這裡的方法是使用兩個優先隊列,即出隊列的順序是按照某種排好序的方式進行的。
所以我們設立兩個優先隊列,這裡叫做堆吧:
1、最大堆,值大的先出來
2、最小堆:值小的先出來

那麼回到我們的問題,我們想想如何確定中位元:
1、假設我們有上述最大堆,最小堆
2、如果我們把進入的所有值較小的一半放到最大堆,較大的一半放到最小堆中,那麼較小的那一半poll出來的,和較大那一半poll出來的,不正好是k個視窗的中位元的候選值麼。
3、按照上面那個思想,我們就行動,再輸入值得時候,根據其大小,放入最大堆或者最小堆中,然後調整一些大小,保證最大堆那邊的大小等於或者多一個於最小堆
4、當輸出的時候,也就是從最大堆取一個,或者雙方各取一個就可以計算了
5、刪除的時候,在對應的堆中刪除,再按照3中的方式更新下就好 2 原題

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.5Given 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]      6Therefore, 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.
3 AC解
public class Solution {    public double[] medianSlidingWindow(int[] nums, int k) {        int n = nums.length;        int m = n - k + 1; // 結果的尺寸        double[] res = new double[m];        //兩個堆,一個最大堆,一個最小        PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, Collections.reverseOrder());        PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(k);        for ( int i=0; i<n; i++){            int num = nums[i];            // 讓maxHeap始終儲存小於一半的值,minHeap儲存大於一半的,正好兩半            if( maxHeap.size() == 0 || maxHeap.peek() >= num)                maxHeap.add(num);            else minHeap.add(num);            // 維護兩個堆,保證兩個堆得大小,要麼保持一致(偶數時),要麼maxHeap多一個(奇數時)            if( minHeap.size() > maxHeap.size() )                maxHeap.add(minHeap.poll());            if( maxHeap.size() > minHeap.size() + 1 )                minHeap.add(maxHeap.poll());            // 如果需要輸出            if ( i-k+1 >=0 ){                if( k % 2 == 1 )                    res[i- k + 1] = maxHeap.peek();                else                     res[i- k + 1] = (maxHeap.peek()/2.0 + minHeap.peek()/2.0); // 小心溢出                //移除並更新                int toBeRemove = nums[i - k + 1];                if( toBeRemove <= maxHeap.peek())                    maxHeap.remove(toBeRemove);                else minHeap.remove(toBeRemove);                // 維護兩個堆,保證兩個堆得大小,要麼保持一致(偶數時),要麼maxHeap多一個(奇數時)                if( minHeap.size() > maxHeap.size() )                    maxHeap.add(minHeap.poll());                if( maxHeap.size() > minHeap.size() + 1 )                    minHeap.add(maxHeap.poll());            }        }        return res;    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.