Problem 204574 · easy · Level 02 Linear Data Structures

Rolling Averages

arrays · sliding-window · running-statistics

Given daily temperatures temps and a window size k, return the average of every block of k consecutive readings, in order. Averages are floats (sum / k). If k is larger than the number of readings, return [].

Examples

Input:  temps = [1, 2, 3, 4, 5], k = 3
Output: [2.0, 3.0, 4.0]

Input:  temps = [1, 2], k = 3
Output: []

Constraints

  • 0 <= len(temps) <= 10**5
  • 1 <= k <= 10**5
  • Target: O(n) time (not O(n * k)).

Goals

  • Compute every window of size k without re-summing from scratch
  • Return an empty list when the window does not fit
Starting Python…