@Maximum value of sword finger offer (python) sliding window

Posted by jj20051 on Tue, 05 Nov 2019 19:24:32 +0100

Sword finger offer brush notes 64 (python)

Title Description

Given the size of an array and sliding window, find out the maximum value of the values in all sliding windows. For example, if the input array {2,3,4,4,2,6,6,2,5,1}andthe size 3 of the sliding window, there are six sliding windows, their maximum values are {4,4,6,6,6,6,6,6,6,5}; for the array {2,3,4,4,4,2,6,6,2,5,5,5,1 {[2,3,4,4,4,4,4,4,2,6,2,5,1 {[2,3,4,2,2,2,2,2,2,2,2,2,5,1}, {2,2,3, [4,2,2,2,6 [4,6], 2,5,5,5,1}, 2,5,{2,3,4, [2,6,2], 5,1}, {2,3,4,2, [6,2,5], 1}, {2,3,4,2,6, [2,5,1]} .

thinking

We can use a queue to get in and out of the queue to realize window sliding, but the elements stored in the queue are not numbers, but the subscripts corresponding to numbers. Here we use a queue to store the subscript of the element that may be the maximum value (we need to know where the window has moved, when the element in the queue is already outside the window, we need to leave the queue). When a new number is encountered, it is compared with the end of the queue element. If it is larger than the end of the queue element, the end of the queue element is lost, and the comparison is repeated until the new number is smaller than the end of the queue element, or the queue is empty, and the new number subscript is put into the queue. At the same time, it needs to judge whether the team head element has left according to the movement of the sliding window.

Code

# -*- coding:utf-8 -*-
class Solution:
    def maxInWindows(self, num, size):
        # write code here
        maxqueue = []
        res = []
        n = len(num)
        if n == 0 or n < size or size == 0:
            return res
        for i in range(n):   
            if len(maxqueue) != 0 and i - size >= maxqueue[0]:
                # Determine whether the team leader element is out of the window. If so, the team leader element is out of the team
                maxqueue.pop(0)
            while maxqueue and num[i] >= num[maxqueue[-1]]:
            # At this stage, the elements in the queue and the corresponding num are all in the sliding window
            # Therefore, if the number after num is larger than the number before num, then the number before num cannot be the maximum,
            # Because the window moves to the right, as long as the front number is included in the scope of the window, the back number must be included,
            # But include the number after, not necessarily the number before.
            # Therefore, the subscript of the number in front of num which is smaller than num[i] in the queue should be out of the queue.
            # Because he can't be the maximum.
                maxqueue.pop()
            maxqueue.append(i)       
            # If the corresponding number of elements in the queue is larger than num[i], I will enter the queue,
            # Because when the elements of the current edge are not in the window, num[i] may be the maximum value of the elements in the window.
            if maxqueue and i >= size -1:
            # Because the length of the window is size, the first size-1 element is not processed
                res.append(num[maxqueue[0]]) 
                # Because in the last cycle, the number of team head elements must be the largest.
                # Press the element corresponding to the team head into the result, and the element will not leave the team in this step,
                # Because the team leader element is not necessarily the left boundary of the window, the next time you move the window,
                # The team leader element may also be in the window and is the largest.
        return res

Topics: Windows Python