Problem 394749 · easy · Phase 03 Linear Management & Searching

Mostly Dry Spells

sliding window · fixed-size window · counting

rain holds the millimetres of rain measured on each day. A block of exactly k consecutive days is a dry spell if at most m of those days had any rain at all (rain[i] > 0). Return the number of dry spells. If k is larger than the number of days, return 0.

Examples

Input:  rain = [0, 3, 0, 0, 2, 0, 0], k = 3, m = 1
Output: 5
Explanation: every block of 3 days contains exactly one rainy day.

Input:  rain = [1, 1, 1, 1], k = 2, m = 0
Output: 0

Constraints

  • 0 <= len(rain) <= 10**5
  • 1 <= k <= 10**5, 0 <= m <= k, 0 <= rain[i] <= 10**4
  • Target complexity: O(n) time; recounting rainy days for every block is too slow for the largest tests.

Goals

  • Count elements satisfying a predicate inside a fixed window
  • Handle windows larger than the input gracefully
Starting Python…