码迷,mamicode.com
首页 > Windows程序 > 详细

leetcode-hard-array-239. Sliding Window Maximum

时间:2019-06-18 13:58:28      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:bsp   maximum   obj   imu   code   The   pre   and   not   

mycode  89.27%

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
  
        if k == 0 or nums == [] or k > len(nums): return []
        
        length = len(nums)
        temp = []
        for j in range(0,k):
            temp.append(nums[j])
        first = max(temp)
        flag = first == nums[0]
        res = [first]
        for i in range(1,length-k+1): 
            if not flag :
                first = max(first,nums[i+k-1])
            else:
                temp = []
                for j in range(0,k):
                    temp.append(nums[i+j])
                first = max(temp)
            res.append(first)
            flag = first == nums[i]
        return res

参考

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        if not nums: return []
        
        # max for the first window
        cur_max = max(nums[:k])
        res = [cur_max]
        left = 0
        
        for right in range(k, len(nums)):
            # recalculate the new max and index as the previous max is not in this window
            if nums[right] > cur_max or nums[right-k] == cur_max :
                cur_max = max(nums[right-k+1: right+1])
               
            res.append(cur_max)
        
        return res

 

leetcode-hard-array-239. Sliding Window Maximum

标签:bsp   maximum   obj   imu   code   The   pre   and   not   

原文地址:https://www.cnblogs.com/rosyYY/p/11044561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!