Skip to content

Commit 6e32c3c

Browse files
Day 9 : Alternating Groups
1 parent 771c1fb commit 6e32c3c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int numberOfAlternatingGroups(vector<int>& colors, int k) {
4+
int n = colors.size();
5+
for(int i = 0; i < k-1; i++){
6+
colors.push_back(colors[i]);
7+
}
8+
9+
// new array
10+
int N = n + (k - 1);
11+
int i = 0, j = 1;
12+
13+
int result = 0;
14+
while(j < N){
15+
if(colors[j] == colors[j-1]){
16+
i = j;
17+
j++;
18+
continue;
19+
}
20+
21+
if(j - i + 1 == k){
22+
result += 1;
23+
i++;
24+
}
25+
26+
j++;
27+
}
28+
29+
return result;
30+
}
31+
};

0 commit comments

Comments
 (0)