79. The maximum value of the sliding window

 

 

Queues two basic operations: enqueue the data into a queue tail ; dequeued from the queue head removed element.

answer:

https://www.acwing.com/solution/acwing/content/853/

First, of course, the most direct approach is to simulate the process of sliding window, sliding to the right every once again have to traverse a number in the window to find the maximum output, such complexity is O (KN)

, we can consider optimizing it. Window slide to the right process is actually the first number in the window to delete , and add a new number to the end of the window, which can be used to simulate two-way queue, each digital tail pop, then a new digital pressed into the head and then find the largest element to the queue.

To find the largest element faster, we can keep those numbers could become the largest element in the queue window only, remove the largest element of a digital window that can not become. Consider a case, if the queue come in a large number, then the queue than this number of smaller numbers can no longer be the largest element in the window, because the large numbers after coming in, would certainly before a small digital window into the early evening to leave the window, so those numbers into the early and relatively small on the "never emerge", so you can pop up the queue.

So we maintain a monotonous two-way queue, the queue is put under standard element . We assume that the deque queue head is the largest element of the queue where the whole index , the value of the element to the tail index represented in descending order. Monotone initial queue is empty. With the traversal of the array, each element before insertion, first of all need to see if the team can head to remain in the queue , if the team from the first subscript i exceeds k, should the team. Second, the need to maintain monotonicity queue, if nums [i] is greater than or equal to the value of the last element team corresponding index, then the tail current is no longer possible / not have the opportunity to act as a maximum value of the sliding window, you need team Last out of the team . Always keep the team element monotonically decreasing from the team head to the tail. Again, traversing the array, each team is the maximum value for each head sliding window where the subscript.

Time complexity analysis: each element up to enqueue dequeue time complexity is O (n)

 

class Solution {
public:
    vector<int> maxInWindows(vector<int>& nums, int k) {
        vector<int>res;//保存答案的数组
        deque<int>q;
        for(int i = 0; i < nums.size(); i++){
            if(!q.empty() && i-q.front() >= k)//判断队头是否需要出队,需要出栈,执行pop_front()
                q.pop_front();
//维护队列单调性,如果队尾元素nums[q.back()]比要添加的元素小,一直删除队尾元素,直至不满足while
while(!q.empty()&&nums[q.back()]<nums[i]) q.pop_back();
       //将要添加的元素的下标添加进队列q队尾。(上一个while维护了队列单调性,保证执行这一句时,插入队尾的元素是队列中的最小值,因为比nums[i]小的都在上一个while中被删除了) q.push_back(i);
if(i >= k-1){//i >= k-1:表示遍历数组长度大于滑动窗口长度 res.push_back(nums[q.front()]);//取队头作为窗口最大元素 } } return res; } }; 作者:cornerCao 链接:https://www.acwing.com/solution/acwing/content/853/ 来源:AcWing 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


 

Guess you like

Origin www.cnblogs.com/make-big-money/p/12348328.html