Maximum value in sliding window

Source: Internet
Author: User

"Requirement" has an integer array arr and a window of size w slide from the leftmost edge of the array to the far right, and the window slides one position at a time to the right. Returns an array of length n-w+1 res,res[i] represents the maximum value for each window state. Take an array of [4,3,5,4,3,3,6,7],w=3] for example. Because the first window [4,3,5] has a maximum value of 5, the second window [3,5,4] has a maximum value of 5, and the third window [5,4,3] has a maximum value of 5. The fourth window [4,3,3] has a maximum value of 4. The fifth window [3,3,6] has a maximum value of 6. The sixth window [3,6,7] has a maximum value of 7. So the final return [5,5,5,4,6,7]. Given a shaped array of arr and its size n, given W, return an array of res. Ensure that W is less than or equal to N, and that the array size is less than or equal to 500. Test sample: [4,3,5,4,3,3,6,7],8,3, return: [5,5,5,4,6,7]. In brief, it is to find the maximum value of sliding window during sliding window.

The more appropriate way of thinking is to use a double-ended queue to act as a sliding window. If we use the ordinary solution, we need to add data to the sliding window each time to search for the maximum value, so the time complexity is O (n*w), if you take a double-ended queue, you can reach the time complexity of O (N). Simply put, we use a double-ended queue arraydeque to act as this sliding window, and the queue's first record is the index of the maximum value of the current window. The following are the entry rules for a double-ended queue:

1. If the queue is empty, the current index goes directly to the queue.

2. If the value J of the end of the queue corresponds to the number of arr[j]>arr[i], then I will also be placed in the tail.

3. If none of the above is true, then the J corresponding to the arr[j]<=arr[i at the end of the queue will need to pop up J, so pop the tail element and repeat the process.

Outbound Rules for queues:

1. If the current team header (that is, the maximum value for the window period) j, is equal to I-w, then the first element of the window has expired out of the window, should be out of the queue, the first element out of the queue.

Maximum value: satisfies the window period, that is, when I already equals or exceeds the window value, the first element of the queue corresponds to ARR[J] is the maximum value of the current window period, you can find the value of arr[j] and put it into the maximum value array.

The whole process is easy to implement after mastering the rules, so the key is to use the double-ended queue.

"Code"

1      Public Static int[] Slide (int[] arr,intNintW) {2             //Write code here3arraydeque<integer> queue =NewArraydeque<integer>();4         int[] result=New int[N-w+1];5         intJ=0;6          for(inti=0;i<n;i++){7             if(Queue.isempty ()) {//queue is empty8 Queue.offerlast (i);9             }Ten             Else if(Arr[queue.peeklast ()]>Arr[i]) { One Queue.offerlast (i); The trailing element of the queue has a value greater than the current value A }//The above can be combined as a judgment -             Else{ - queue.polllast (); thei--; Otherwise the pop-up tail element repeats the above procedure, and the actual purpose is to find the position of the current element in its correct order in the window -                 Continue; -             } -             if(Queue.peekfirst () ==i-W) { + Queue.pollfirst (); Determine if the window's first position expires -             } +             if(i>=w-1) { Aresult[j++]=Arr[queue.peekfirst ()]; Put into the result array at             } -         } -         returnresult; -}

The implementation of the special is the use of the double-ended queue arraydeque, relative to the basic queue, its basic operations can be divided into

Entry: Offerlast (), Offerfirst ();

Team: Polllast (), Pollfirst ();

Team head: Peekfirst ()

Team Tail: Peeklast ()

  

Maximum value in sliding window

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.