Python with a fast algorithms ,about 224ms ,and welcome to talk about some more faster method

  • 0
     class Solution:
            # @param {integer[]} nums
            # @param {integer} k
            # @return {integer[]}
            def maxSlidingWindow(self, nums, k):
                __start = 0
                __list = []
                    max_ = max(nums[__start:__start+k])
                    index = nums.index(max_)
                except ValueError:
                    return []
                while __start + k <= len(nums):
                        max_ = nums[__start+k-1] if nums[__start+k-1] > max_ else max_
                        if __start == index and __start +k < len(nums):
                            now = nums[__start+1:__start+1+k]
                            max_ = max(now)
                            index = __start+1+now.index(max_)
                        __start += 1
                return __list

    We can see that shouldn't compare each list slice,just only compare the next-into value if larger than last max value. Certainly we must think some especially cases.
    Finally, your can find more funny thing about this web OJ on "",-------【python】大型刷题系列

  • 0

    I think try..except drags down the runtime most of the time. And it's not a good way to handle corner cases.

  • 0

    Yes,you are right, thanks.

Log in to reply

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