Maximum Average Subarray I: the skills you need first
Updated
LeetCode · easy · sliding window. Seed k cells, then slide. Max average is max sum over a fixed length.
Skills this problem builds on
Stuck on Maximum Average Subarray I? It is usually one of these. Get each one solid and the solution stops being a trick.
- Fix the window length in advance
k is given. Every window has length k. You do not discover length. You slide a block of k cells.
- Slide by dropping left and adding right
After the first window [0,k), each step i from k to n-1 does: drop a[i-k], add a[i]. L and R both increase by 1. Length stays k.
- Maintain the sum of the window
Window sum updates in O(1) per move: add a[R] when R advances, subtract a[L] when L advances. After every move, sum equals a[L]+…+a[R].
check which ones you have 10 minutes, free, no account.