Leetcode 480. Sliding window Median Sliding windows in the middle number problem-solving report

Source: Internet
Author: User
1 ideas for solving problems

The title will give an array, and a sliding window of the size K, let you find out when this window slides in the process, this k window of the median number is respectively.

The most naive way is to sort in the K window, not explained here (because the overhead is very large, (n-k+1) * (K*log (k)).

The approach here is to use two priority queues, where the order of the queues is performed in a sort of orderly fashion.
So we set up two priority queues, called Heaps:
1, the largest heap, the value of large first out
2, the smallest heap: the value of small first out

So back to our question, let's think about how to determine the median:
1. Suppose we have the maximum heap above, the smallest heap
2, if we put all the value of the smaller half into the largest heap, the larger half into the smallest heap, then the smaller half of the poll out, and the larger half of the poll out, not exactly the K-window of the median number of candidates.
3, according to the above thought, we act, and then input is worth, according to its size, put into the maximum heap or the smallest heap, and then adjust some size, to ensure that the maximum size of the heap is equal to or more than one in the smallest heap
4, when the output, that is, from the largest heap of one, or both sides take one can be calculated
5, delete the time, in the corresponding heap to delete, and then in 3 of the way to update the 2 original question

Median is the middle value of ordered integer list. If the size is even and 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 are (2 + 3)/2 = 2.5 Given An array nums, there are a sliding wind ow of size k which is moving from the very left of the array to the very right. You can only be the K numbers in the window. 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

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. Always valid, ie:1≤k≤input array ' s size for non-empty array. 
3 AC Solution
public class Solution {public double[] Medianslidingwindow (int[] nums, int k) {int n = nums.length; int m = n-k + 1;
        Size of the result double[] res = new DOUBLE[M]; Two heaps, one Max heap, one minimum priorityqueue<integer> maxheap = new priorityqueue<integer> (k, Collections.reverseord
        ER ());
        priorityqueue<integer> minheap = new priorityqueue<integer> (k);
            for (int i=0; i<n; i++) {int num = nums[i];
                Let Maxheap always save less than half of the value, Minheap save more than half, just two halves if (maxheap.size () = 0 | | maxheap.peek () >= num)
            Maxheap.add (num);
            else Minheap.add (num); Maintain two heaps, guarantee two heap size, either remain consistent (even), or maxheap one (odd-time) if (Minheap.size () > Maxheap.size ()) Maxhe
            Ap.add (Minheap.poll ());
            if (Maxheap.size () > Minheap.size () + 1) minheap.add (Maxheap.poll ()); If you need to output 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);
                Beware of overflow//removal and update int toberemove = nums[i-k + 1];
                if (Toberemove <= maxheap.peek ()) Maxheap.remove (Toberemove);
                else Minheap.remove (toberemove);
                    Maintain two heaps, guarantee two heap size, either remain consistent (even), or maxheap (odd-numbered) if (Minheap.size () > Maxheap.size ())
                Maxheap.add (Minheap.poll ());

            if (Maxheap.size () > Minheap.size () + 1) minheap.add (Maxheap.poll ());

    } return res; }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.