Skip to content
LC-0643 Easy LeetCode

643. Maximum Average Subarray I

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 45% Topics: Array, Sliding Window
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def findMaxAverage(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: float
        """
        result = total = sum(nums[:k])
        for i in xrange(k, len(nums)):
            total += nums[i] - nums[i-k]
            result = max(result, total)
        return float(result) / k

Solution from kamyu104/LeetCode-Solutions · MIT