Maximum Average Subarray I

Problem Id: 643 Difficulty: Easy


Intuition

Solution


class Solution:
    def findMaxAverage(self, nums: List[int], k: int) -> float:
        s = 0
        for i in range(k):
            s += nums[i]
        total = s
        for i in range(k, len(nums)):
            s += nums[i]
            s -= nums[i - k]
            total = max(s, total)
        return total / k