Time Limit Exceeded - python sliding window


  • 0
    J

    Hello,

    I'm new to algorithm problems and can't understand why my solution would exceed the time limit?

    I think that it wouldn't even be O(n) since it only iterates over the list len(nums) / k times?

    How am I incorrect in my thinking?

    Thanks!

    Here's the code:

    def findMaxAverage(self, nums, k):
    
        sums = []
        i = 0
        numslen = len(nums)
        while i + k <= numslen:
            sums.append(sum(nums[i:i+k]))
    	i += 1
          
        return max(sums) / k
    

Log in to reply
 

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