-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1248.cpp
More file actions
37 lines (28 loc) · 774 Bytes
/
1248.cpp
File metadata and controls
37 lines (28 loc) · 774 Bytes
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<iostream>
#include<vector>
#include<unordered_map>
int atMost(const std::vector<int>& nums, int k) {
int oddCount = 0;
int subArrayCount = 0;
int left = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] % 2 == 1) ++oddCount;
while (oddCount > k) {
if (nums[left] % 2 == 1) --oddCount;
++left;
}
subArrayCount += i - left + 1;
}
return subArrayCount;
}
int numberOfSubarrays(std::vector<int>& nums, int k) {
return atMost(nums, k) - atMost(nums, k - 1);
}
int main() {
// std::vector<int> nums = { 1,1,2,1,1 };
std::vector<int> nums = { 2,2,2,1,2,2,1,2,2,2 };
// int k = 3;
int k = 2;
std::cout << numberOfSubarrays(nums, k);
std::cin.get();
}