239. 滑动窗口最大值

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

返回 滑动窗口中的最大值 

示例 1:

输入: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
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        #从头到尾遍历 选取在k个范围里面的最大值
        result=[]
        count=k
        #co1=k
        temp=0
        for i in range(len(nums)-k+1):
            count =k
            while count:
                wind=nums[i:i+k]
                count=0
                '''result.append(nums[i+count])
                count +=1
            temp=max(result)'''
            #temp=max(wind)
            result.append(max(wind))
            '''hile count >=0:
                result.pop()
                count -=1
            result.append(temp) '''
        return result    

        

上面是暴力的做法 导致了超时 下面我们尝试一下别的做法 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值