backbenchcode
← all problemsnext: Merge Overlapping Intervals
#03MediumArrays & Strings · 52% acceptance

Rolling Window Mean

Sliding WindowArrayMath

Given an array `nums` and an integer `k`, find the contiguous window of length `k` with the highest average value and return that average.

If you have ever smoothed a noisy time-series with a moving average, this is that — turned into an O(n) algorithm.

Examples

Input: nums = [1,12,-5,-6,50,3], k = 4

Output: 12.75

Note: The window [12,-5,-6,50] averages 12.75.

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
6
7
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.