LeetCode: Maximum Average Subarray I

Find the contiguous subarray of given length that has the maximum average value.

Posted on LeetCode Python

See original problem on leetcode.com

Difficulty: Easy

Solution

Calculate the sum of the first k elements.

Use a sliding window approach to iterate through the array starting from index k. For each new element added to the window, subtract the element that is leaving the window (i.e., the element at index i - k). Update the current sum and check if it’s greater than the greatest sum found so far.

Return the greatest sum divided by k to get the maximum average.

Runtime

40 ms | Beats 94.36%

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