"Sword Finger Offer" Brush Question Series-(52) Maximum Sliding Window

topic

Given an array nums and the size of the sliding window k, please find the maximum value among all sliding windows.
Example:
Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]

Ideas

Queue is a double-ended queue, the head of the queue always stores the maximum value of each step; res stores the maximum value of all sliding windows.

Start traversing the nums array, which involves a total of two processes:
when the number in the window is less than or equal to k , save a number, before saving a number, first determine whether the number at the end of the queue is less than the number to be saved, if it has Some numbers are less than the number to be stored, then these numbers can no longer be the maximum value of the sliding window, delete them from the queue.
When the number in the window is greater than k , the window can be formed. The operation in the queue is the same as the first process, except that a judgment is added. When the element at the head of the queue slides out of the window, the number that slides out needs to be deleted from the head of the queue.

The reason why the two processes cannot be merged is that the value of the res list will not be updated because the first process has not formed a window. And the value of res needs to be updated every time in the second process.

Code

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        if not nums or k==0: return []
        queue = collections.deque()
        res = []
        
        for i in range(k):
            while queue and nums[i]>queue[-1]:
                queue.pop()   
            queue.append(nums[i])

        res.append(queue[0])

        for i in range(k,len(nums)):
            if queue[0]==nums[i-k]:
                queue.popleft()
            while queue and nums[i]>queue[-1]:
                queue.pop()    
            queue.append(nums[i])
            res.append(queue[0])

        return res

the complexity

Time complexity O(n)
Space complexity O(k)

supplement

The maximum value of the queue , write code.

class Solution:

    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        stack1 = []  ## 保存滑动窗口里的值
        stack2 = []  ## 单调队列,队列头部始终是当前滑动窗口的最大值
        res = []     ## 滑动窗口的最大值列表
        if not nums or k<1: return []
    
        for num in nums[:k]:
            stack1.append(num)
            while stack2 and num>=stack2[-1]:
                stack2.pop()
            stack2.append(num)

        res.append(stack2[0])

        for num in nums[k:]:
            # 删除队列的头部元素
            if stack1[0]==stack2[0]:
                stack2.pop(0)
            stack1.pop(0)
            # 添加新元素
            stack1.append(num)
            while stack2 and num>stack2[-1]:
                stack2.pop()
            stack2.append(num)
            # 保存当前最大值
            res.append(stack2[0])
        return res
    

Guess you like

Origin blog.csdn.net/weixin_44776894/article/details/107423741