Exact Storm Stretches
Medium · sliding window
A weather station records the rainfall of every day, in millimetres, in the list rain. A day is stormy if its rainfall is at least heavy millimetres.
A stretch is a run of days that follow each other with none skipped. A farmer wants to know how many stretches contain exactly k stormy days.
Return that number. Two stretches are different if their first or last day differs, even if they list the same rainfall. A stretch of one day counts. If no stretch has exactly k stormy days, return 0. Note that k can be 0, which asks for stretches with no stormy day at all.
Examples
Example 1
Input: rain = [1, 6, 1, 6, 1], heavy = 5, k = 2
Output: 4
The stretches are days 1 to 3, 0 to 3, 1 to 4 and 0 to 4, each with both stormy days (days 1 and 3) and nothing else stormy.
Example 2
Input: rain = [2, 3], heavy = 5, k = 0
Output: 3
No day is stormy, so all three stretches (two single days and the pair) have zero stormy days.
Example 3
Input: rain = [9], heavy = 5, k = 1
Output: 1
Constraints
- 1 <= len(rain) <= 10^5
- 0 <= rain[i] <= 10^4
- 0 <= heavy <= 10^4
- 0 <= k <= 10^5