239. The sliding window maximum / deque / queue monotonous

Deque

class deque {
   // 在队头插入元素 n
   void push_front(int n);
   // 在队尾插入元素 n
   void push_back(int n);
   // 在队头删除元素
   void pop_front();
   // 在队尾删除元素
   void pop_back();
   // 返回队头元素
   int front();
   // 返回队尾元素
   int back();
}如果用链表实现,

The complexity of the operation are O (1)

Python3 call in

import collections
d = collections.deque()

append (add an element to the right)

import collections
d = collections.deque()
d.append(1)
d.append(2)
print(d)

Output: deque ([1, 2])

appendleft (add an element to the left)

clear (empty queue)
Copy (shallow copy)
COUNT (returns the number of occurrences of the specified element)
Extend (expansion of a list from the right queue element)
extendleft (from left extended a list of queue elements)
index (an index to find the location of an element )
iNSERT (insert elements at the specified position)

pop (far right acquisition element, and delete the queue)

popleft (leftmost get an element in the queue and delete)

remove (delete the specified element)
 abbreviated: the right end of the same operation as in list
To getleft and getright
directly with d [0], d [-1 ]
is determined whether the air:
IF represents d if d is not empty
if not d If d is null expressed

From this we can see that the object container inherited from the list, the list contains all the features

Monotonically decreasing queue

class MonotonicQueue {
   // 在队尾添加元素 n
   void push(int n);
   // 返回当前队列中的最大值
   int max();
   // 队头元素如果是 n,删除它
   void pop(int n);
}

Its operation is very little, if implemented with the deque
which int max () is deque.left () python, i.e. deque [0], because it is dull
in order to maintain monotonicity is rewritten to delete the first push all the right than the insertion value smaller number

class MonotonicQueue {
private:
    deque<int> data;
public:
    void push(int n) {
        while (!data.empty() && data.back() < n) 
            data.pop_back();
        data.push_back(n);
    }
};

And this problem is not to be transferred directly to the packet queue monotonous, but in deque is based, the implementation of a queue monotonically with thoughts come

Look at this question

Given an array nums, have a size of k rightmost sliding window moves from the leftmost array to array. You can only see k digits in the sliding window. A time sliding window moves to the right one.

Returns the maximum value in the sliding window.

Example:

Input: nums = [1,3, -1, -3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]
Explanation:

 Position of the maximum sliding window

---------------               -----

[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

prompt:

You may assume that k always effective, in the case of the input array is not empty, 1 ≤ k ≤ size of the input array.

Advanced:

Can you in linear time complexity of solving this problem it?

Python solution to a problem (by leetcode)

Look at the video programmer northeast

import collections
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        if not nums: return []
        d = collections.deque()
        res = []
        for i in range(len(nums)):
            # 如果队头正好就离开了滑动窗口,pop它
            if d and d[0] == i - k:
                d.popleft()
            # 单调队列.push()
            while d and nums[d[-1]] < nums[i]:
                d.pop()
            d.append(i)
            if i + 1 >= k: 
                # 窗口开始滑动
                res.append(nums[d[0]])
        return res

Pictures .png

time complexity

Logically time complexity is O (n), if in c ++
but python inherent disadvantage, deque inherited from the list, list array is a bottom, so the operation can not O (n), the worst case time complexity is O (n2)
even so, we still need to know the algorithm for later use in other languages can do out
space complexity worst-case O (n)

Copy of problem solution

Author: labuladong
link: https://leetcode-cn.com/problems/sliding-window-maximum/solution/dan-diao-dui-lie-by-labuladong/

Guess you like

Origin www.cnblogs.com/Akarinnnn/p/12080051.html