MAx Avg Subarray

mail@pastecode.io avatar
unknown
python
2 years ago
494 B
2
Indexable
Never
class Solution:
    def findMaxAverage(self, nums: List[int], k: int) -> float:
        start = 0
        curAvg = 0
        maxAvg = float("-inf")
        sum = 0
        
        for end in range(len(nums) - 1):        # traversal of List sequence
            sum += nums[end]
            
            if end - start >= k - 1:
                curAvg = sum/k
                maxAvg = max(curAvg, maxAvg)
                sum -= nums[start]
                ++start
        
        return maxAvg