1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: int numberOfSubarrays(vector<int>& nums, int k) { int n = nums.size(); function<int(int)> count = [&](int lower) { int res = 0; for (int left = 0, right = 0, cnt = 0; right < n; ++right) { if (nums[right] & 1) ++cnt; while (cnt > lower) if (nums[left++] & 1) --cnt; res += right - left + 1; } return res; }; return count(k) - count(k - 1); } };
|