Simple Readable Python Solution

  • 0
        def findMaxAverage(self, nums, k):
            :type nums: List[int]
            :type k: int
            :rtype: float
            running_sum = float(sum(nums[:k]))
            running_avg = running_sum / k
            max_avg = running_avg
            for i in xrange(k,len(nums)):
                running_sum += nums[i]
                running_sum -= nums[i-k]
                running_avg = running_sum / k
                max_avg = max(running_avg, max_avg)
            return max_avg

Log in to reply

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