天天看点

[LeetCode Python3] 239. Sliding Window MaximumPython3 Solution:

Python3 Solution:

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        queue = []
        left, right = 0, 0
        res = []
        while right < len(nums):
            num = nums[right]
            right += 1
            index = 0
            while index < len(queue):
                if queue[index] < num:
                    index += 1
                else:
                    break
            queue = [num] + queue[index:]
            
            while right - left == k:
                res.append(queue[-1])
                num = nums[left]
                left += 1      
                if num == queue[-1]:
                    queue.pop()
                
        return res
           

继续阅读