-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
45 lines (37 loc) · 1.05 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
int n = nums.size();
vector<int> odd;
for (int i = 0; i < n; ++i) {
if (nums[i] & 1) odd.push_back(i);
}
int result = 0;
for (int l = 0; l + k - 1 < odd.size(); ++l) {
int r = l + k - 1;
int a = -1;
if (l - 1 >= 0) a = odd[l - 1];
int b = n;
if (r + 1 < odd.size()) b = odd[r + 1];
result += (odd[l] - a) * (b - odd[r]);
}
return result;
}
};
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
int n = nums.size();
vector<int> odd = {-1};
for (int i = 0; i < n; ++i) {
if (nums[i] & 1) odd.push_back(i);
}
odd.push_back(n);
int result = 0;
for (int l = 1; l + k - 1 < odd.size() - 1; ++l) {
int r = l + k - 1;
result += (odd[l] - odd[l - 1]) * (odd[r + 1] - odd[r]);
}
return result;
}
};