-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3208.cpp
More file actions
33 lines (33 loc) · 900 Bytes
/
3208.cpp
File metadata and controls
33 lines (33 loc) · 900 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
class Solution {
public:
int numberOfAlternatingGroups(vector<int>& colors, int k) {
int res = 0;
int n = colors.size();
for (int i = 0; i < n; ++i) {
colors.push_back(colors[i]);
}
int right = 0;
int prev = -1;
for (int left = 0; left < n; ++left) {
if (right <= left) {
right = max(right, left);
prev = -1;
}
while (right - left < k) {
if (prev == -1) {
prev = colors[right];
right++;
}
else {
if ((prev ^ colors[right]) == 0) break;
prev = colors[right];
right++;
}
}
if (right - left == k) {
res++;
}
}
return res;
}
};