Skip to the code
cf.
+10 XP

#125 Frame Time Moving Average

easy

A performance overlay draws a smooth line through frame times. Each point of the line is the average of k frame times in a row.

Write movingAverage(frameTimes, k) that returns an array with the average of every window of k consecutive frame times, from the first window to the last. If there are fewer than k frame times, return []. Answers within 0.00001 of the exact value are accepted.

Example 1

Input: frameTimes = [16,17,33,16,18], k = 2
Output: [16.5,25,24.5,17]

For example the first window is (16 + 17) / 2 = 16.5.

Example 2

Input: frameTimes = [10,20], k = 3
Output: []

There aren't enough frames for one window.

Constraints

  • 0 <= frameTimes.length <= 100000
  • 1 <= k <= 1000
  • 0 <= frameTimes[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).