239. Sliding Window Maximum

Source: Internet
Author: User

Given an array nums, there is a sliding window of size K which are moving from the very left of the array To the very right. You can only see the K numbers in the window. Each of the sliding window moves right by one position.

for example,
given  nums  =  [1,3,-1,-3,5,3,6,7] k  =3.

Window position                Max---------------               -----[1  3  -1]-3  5  3  6  7       3 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       5 1  3  -1  -3 [5  3  6] 7       6 1  3  -1  -3  5 [3  6  7]      7

therefore, return the max sliding window as  [3,3,5,5,6,7] .

note: 
you could assume  k  is always valid, ie:1≤k≤input array ' Ssiz E for non-empty array.

Follow up:
Could solve it in linear time?

Hint:

  1. How about using a data structure such as deque (double-ended queue)?
  2. The queue size need not being the same as the window ' s size.
  3. Remove redundant elements and the queue should store only elements that need to be considered.

Idea: Follow the prompts, using deque to store array subscript, keep Deque's head always store the maximum number of the current window subscript. Each cycle, the number of deque less than nums[i] is excluded. Add the subscript of the right edge of the window to the deque, while rejecting the ordinal of the deque head crossing. When the number of traversed has reached the size of the window, the subscript of the number of the deque head is the subscript of the largest number of the current window, adding this number to the array that is eventually returned.

classSolution { Public: Vector<int> Maxslidingwindow (vector<int>& Nums,intk) {vector<int>ret; Deque<int>Q;  for(intI=0; I<nums.size (); i++){            //Keep the maximum number only in Deque             while(!q.empty () &&nums[i]>=Nums[q.back ()]) q.pop_back (); //each time you move the window, add IQ.push_back (i); //Remove the front from the border            if(Q.front () <=i-k) Q.pop_front (); //when the window size is reached, add the maximum value in the deque to the RET array            if(i>=k-1) Ret.push_back (Nums[q.front ()); }        returnret; }};



239. Sliding Window Maximum

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.