362. Maximum sliding window
Topic
Give an array of integers that may contain duplicates, and a sliding window of size k, slide the window from left to right in the array, and find the maximum value within each window in the array.
Examples
to the array [1,2,7,7,8], the sliding window size is k = 3. returns [7,7,8].
Explain:
At the beginning, the state of the window is as follows:
[|1, 2, 7|, 7, 8], with a maximum value of 7;
Then the window moves one to the right:
[1, +, 7, 7|, 8], with a maximum value of 7;
The last window then moves to the right one:
[1, 2, |7, 7, 8|], the maximum is 8.
train of ThoughtNew Ket Network Algorithm video has to explain this type of problem; method one: Each window to traverse each of the K number, select the maximum; method Two: Use two-terminal queue to achieve maximum update
two-terminal queue
Defining a two-terminal queue (deque, full name Double-endedqueue) is a data structure that has the nature of queues and stacks. Elements in a two-terminal queue can be ejected from both ends, and their qualifying insert and delete operations are performed at both ends of the table.
Action: Join from the right, eject from the left
The operation function of two-terminal queues in Python;
Call the module first
Import deque class from collections import deque #首先从collections module
The
defines a two-terminal queue and Operation A=deque ([]) #创建一个空的双队列 a.append (n) #从右边像队列中增加元素, n represents an increased element A.appendleft (n) #从左边像队列中增加元素, and n represents an added element a.clear () #清空队列 a.count (n) #在队列中统计元素的个数, n represents the statistical element A.extend (n) #从右边扩展队列, n represents the extended queue A.extendleft (n) #从左边扩展队列, n represents the extended queue A.pop () # Deletes the element from the right of the queue and returns the deletion value A.popleft () #从队列的左边删除元素, and returns the Delete value method Two idea defines a two-terminal queue Qmax, which holds the subscript in the nums of the array, the initial qmax.append (0) Define a subscript and nums value that holds the current nums[1:] For each window maximum res, x,y, initially 1,nums[1] to determine the subscript J of the Qmax team tail store, if nums[j] > y, and put subscript x directly into the tail of the Qmax team; If NUMS[J] <=y, it pops up from the tail of the Qmax until a subscript in the Qmax corresponds to a value greater than Y, at which point the X is placed at the end of Qmax's team. Perform step 3 to determine if there is a number that is out of date (not within the current K range) in Qmax's team head, Popleft () deletes the team head, and then stores the maximum value of the current window, which is Qmax's team head in Res. Make x = X+1 nums[y+1], perform steps 3 and 4 until the last subscript and value of Nums are reached. Code
From collections Import Deque class Solution: "" @param: nums:a List of integers @param: k: An integer @ Return:the Maximum number inside the window at each moving "" "Def Maxslidingwindow (self, nums, k): # WRI
Te your code here #特殊情况, K=1, then each is the largest if k = = 1:return Nums #新建一个双端队列qmax with an initial value of 0 #res存放每次移动的最大结果 Qmax = Deque ([]) qmax.append (0) res = [] for x,y in enumerate (nums[1:],1)
: "" "" "X,y represents the current subscript and nums value, judging the subscript J of the Qmax team tail, if nums[j] > y, put subscript x directly into the tail of the Qmax team;
If NUMS[J] <=y, it pops up from the tail of the Qmax until a subscript in the Qmax corresponds to a value greater than Y, at which point the X is placed at the end of Qmax's team. "" If NUMS[QMAX[-1]] <=y:for i in range (len (Qmax) -1,-1,-1): If Nums[q
Max[i]] > Y:break else:qmax.pop () Qmax.append (x) #当qmax存在Expired data, that is, not in the move K range, removes it from the two-terminal queue if qmax[0] <= x-k: Qmax.popleft () #将每次移动窗口的最大值存储到res中 If x >=k-1: Res.append (nums[qmax[0]]) return res