Simple and straightforward Python solution


  • 0
    X
    class Solution(object):
        def maxSlidingWindow(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            if len(nums) == 0 or k == 0:
                return []
            if k == 1:
                return nums
            if k == len(nums):
                return [max(nums)]
            currmax = max(nums[:k])
            res = [currmax]
            if nums[0] == currmax:
                currmax = max(nums[1:k])
            for i in range(k,len(nums)):
                if nums[i] > currmax:
                    currmax = nums[i]
                    res.append(currmax)
                else:
                    res.append(currmax)
                    if nums[i-k+1] == currmax:
                        currmax = max(nums[i-k+2:i+1])
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.