Count Number of Nice Subarrays LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> nums;
4
int solve(int k) {
5
int low = 0, high = 0, cnt = 0, res = 0;
6

7
while (high < nums.size()) {
8
if (nums[high] & 1) {
9
cnt++;
10

11
while (cnt > k) {
12
if (nums[low] & 1) cnt--;
13
low++;
14
}
15
}
16
high++;
17
res += high - low;
18
}
19

20
return res;
21
}
22
int numberOfSubarrays(vector<int> &nums, int k) {
23
this->nums = nums;
24
return solve(k) - solve(k - 1);
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0