Problem intuition
- The brute-force version recalculates each window sum from scratch.
- The sliding-window version keeps the current sum and updates it by removing the outgoing number and adding the incoming one.
Find the maximum average among all contiguous subarrays of size k.
Sliding WindowThe solutions below are ordered from least optimal to most optimal, so you can see the improvement path instead of only the final answer.
Solution 1
Solution 2