Problem 311878 · easy · Phase 03 Linear Management & Searching

Windows Over the Threshold

sliding window · fixed-size window · counting

A sensor produces integer readings. An alarm fires for every block of exactly k consecutive readings whose average is greater than or equal to threshold. Return how many such blocks exist. If k is larger than the number of readings, return 0.

Examples

Input:  readings = [2, 2, 2, 2, 5, 5, 5, 8], k = 3, threshold = 4
Output: 3
Explanation: the blocks [2,5,5], [5,5,5] and [5,5,8] have averages 4, 5 and 6.

Input:  readings = [11, 13, 17, 23, 29, 31, 7, 5, 2, 3], k = 3, threshold = 5
Output: 6

Constraints

  • 0 <= len(readings) <= 10**5
  • 1 <= k <= 10**5, 0 <= readings[i], threshold <= 10**4
  • Target complexity: O(n) time; an O(n·k) re-sum per window is too slow for the largest tests.

Goals

  • Slide a fixed window while keeping its sum up to date
  • Compare an average with a threshold using integer arithmetic
Starting Python…