generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
36 lines (31 loc) · 808 Bytes
/
solution.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
#include <vector>
class Solution {
public:
int numberOfSubarrays(std::vector<int>& nums, int k) {
std::vector<int> evens(k);
int evensIndex{k - 1};
int right = nums.size();
int left{right - 1};
while (left >= 0 && evensIndex >= 0) {
if (nums[left] % 2 != 0) {
evens[evensIndex] = right - left;
--evensIndex;
right = left;
}
--left;
}
if (evensIndex >= 0) return 0;
evensIndex += k;
int totalCount{0};
while (left >= 0) {
if (nums[left] % 2 != 0) {
totalCount += (right - left) * evens[evensIndex];
evens[evensIndex] = right - left;
if (--evensIndex < 0) evensIndex += k;
right = left;
}
--left;
}
return totalCount + (right - left) * evens[evensIndex];
}
};