Skip to main content

🔴 剑指 Offer 59 - I. 滑动窗口的最大值

LeetCode 提示

题目难度 困难

原题链接 🔗 leetcode

题解 1_抄了新东西.py#

import collections
class Solution:    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:        if not nums:            return []        # deque是单调递减队列        deque = collections.deque()                for idx in range(k):            while deque and deque[-1] < nums[idx]:                deque.pop()            deque.append(nums[idx])        res = [deque[0]]
        for idx in range(k, len(nums)):            if deque and deque[0] == nums[idx-k]:                deque.popleft()            while deque and deque[-1] < nums[idx]:                deque.pop()            deque.append(nums[idx])            res.append(deque[0])                return res