Simple and straightforward Python solution

  • 0
    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]
                    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.