Skip to main content
  1. LeetCode/

LeetCode 643: Maximum Average Subarray I

·1 min· ·
LeetCode Daily Easy Sliding Window
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-08-02

  • Total time: 00:00.00

Intuition
#

This is a fixed window size sliding window problem. Given the problem requirements, we need to find the maximum average of a subarray. We can understand this as finding the maximum sum of a subarray and then dividing by the window size to get the average. Therefore, the target can be simplified to a maximum subarray sum problem.

Approach
#

class Solution:
    def findMaxAverage(self, nums: List[int], k: int) -> float:
        max_sum = 0
        for i in range(k):
            max_sum += nums[i]

        sum_now = max_sum
        for i in range(k, len(nums)):
            sum_now += nums[i]
            sum_now -= nums[i - k]

            max_sum = max(max_sum, sum_now)

        return max_sum / k

Findings
#

This is a fixed window problem. We can accumulate the first window first, and then slide the window until the end.

Encountered Problems
#

Related

LeetCode 2090: K Radius Subarray Averages
·2 mins
LeetCode Daily Medium Sliding Window
LeetCode Problem Solving
LeetCode 1399: Count Largest Group
·1 min
LeetCode Daily Easy Hash Map
LeetCode Problem Solving
LeetCode 724: Find Pivot Index
·2 mins
LeetCode Daily Easy Prefix Sum
LeetCode Problem Solving
LeetCode 1534: Count Good Triplets
·3 mins
LeetCode Daily Easy Prefix Sum
LeetCode Problem Solving
LeetCode 2843: Count Symmetric Integers
·1 min
LeetCode Daily Easy Digit DP
LeetCode Problem Solving
LeetCode 617: Merge Two Binary Trees
·2 mins
LeetCode Daily Easy Tree
LeetCode Problem Solving