Monday, October 3
Shadow

[Maximum Average Subarray I]LeetCode 654 #SlidingWindow

class Solution:
    def findMaxAverage(self, nums: List[int], k: int) -> float:
        max_avg, win_start, win_sum = -inf, 0, 0
        
        for i in range(len(nums)):
            win_sum += nums[i]
            
            if i >= k - 1:
                max_avg = max(max_avg, win_sum)
                win_sum -= nums[win_start]
                win_start += 1
        
        return max_avg / k

Leave a Reply