Counting Subarrays (At-Most-K)
Given an array of sales, count the number of subarrays with at most k bad days (days with fewer than 10 sales).
Key Insight: If a window [l, r] is valid (has <= k bad days), then all subarrays ending at r within this window (i.e., starting from l, l+1, ..., r) are also valid. There are r - l + 1 such subarrays.
l
3
0
20
1
5
2
2
3
8
4
count=0
bad_days=0
k=1
Step 1 / 16
Step 1:
Initialize: l=0, count=0, bad_days=0. k=1.
Pointers: l=0, r=-1
Focus: default
count=0bad_days=0k=1