bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 643. Maximum Average Subarray I

643. Maximum Average Subarray I

Leetcode Solutions

Sliding Window Approach

  1. Initialize max_sum as the sum of the first k elements.
  2. Initialize window_sum with the same value as max_sum.
  3. Iterate from index k to the end of the array. a. Update window_sum by subtracting the element that is left out of the window (nums[i - k]) and adding the new element that enters the window (nums[i]). b. Update max_sum with the maximum of max_sum and the new window_sum.
  4. Return max_sum / k as the maximum average.
UML Thumbnail

Cumulative Sum Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...