LeetCode-Python-239. 滑动窗口最大值

给定一个数组 nums,有一个大小为 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。

返回滑动窗口最大值。

示例:

输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7] 
解释: 

  滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

注意:

你可以假设 总是有效的,1 ≤ k ≤ 输入数组的大小,且输入数组不为空。

进阶:

你能在线性时间复杂度内解决此题吗?

第一种思路:

暴力求解。

每次把window数组找出来然后暴力求最大值。

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """

        if not nums:
            return []

        temp,res = list(), list()
        for index in range(len(nums) - k + 1):
            temp = nums[index: index + k]
            max_val = max(temp)
            
            res.append(max_val)
        
        return res
            

第二种思路:

在第一种思路上小小的改进一下,每一次循环不需要重新算整个temp,而是把队头pop,然后把新的元素压入队尾。

会快100ms。

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        # i 从0 滑到 l - k  + 1
        if not nums:
            return []
        temp,res = list(), list()
        for index in range(len(nums) - k + 1):
            
            if index == 0:
                temp = nums[index: index + k]              
            else:
                temp.pop(0)
                temp.append(nums[index + k - 1])
                
            max_val = max(temp)            
            res.append(max_val)
        
        return res
            

第三种思路:

Sliding Window法。

维护window这个数组,使得在任意时刻,window【0】都是当前Window内最大值的下标。

并且window从左到右恰恰是第一大、第二大、。。。。。第K大的元素

维护过程为:

(Window代表窗口, window代表窗口的数组)

1. 如果window不为空,并且 window[0] 比 i - k 小 (这就说明window[0]在当前Window的左界的左侧,应该被踢出去), 把window【0】弄出去

2. 把从队尾倒着数的每个比item 大的元素都弄出去

3. 把item 弄进Window

4. 如果index 》 = k - 1, 就说明Window size 已经有k了,可以输出答案了。因为如果 index < k - 1, 说明Window size还没到k。

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        if not nums:
            return []
        
        window,res = list(), list()
        for index, item in enumerate(nums):
            # temp = nums[index: index + k]
            
            if window and window[0] <= index - k: #  window是存在的, 后面的条件保证window[0] 不在当前的window中,应该被滑出
                window.pop(0)
            
            while window and nums[window[-1]] <= item: # 这两行在维护这个window, 使得所有比item小的元素的下标都被弹出
                window.pop()
                
            window.append(index) # 把index 压进window
            
            if index >= k - 1:# 如果window的size已经达到了K, 就压入res, 如果index < k - 1,就代表当前window还不够长
                res.append(nums[window[0]])
        
        return res
            

猜你喜欢

转载自blog.csdn.net/qq_32424059/article/details/88689573