File tree 1 file changed +50
-0
lines changed
1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+
3
+ class Solution {
4
+ public:
5
+ int numberOfAlternatingGroups (std::vector<int >& colors, int k) {
6
+ int n = colors.size ();
7
+ if (k > n) return 0 ;
8
+
9
+ int count = 0 ;
10
+
11
+
12
+ bool check = true ;
13
+ for (int i = 0 ; i < k - 1 ; ++i) {
14
+ if (colors[i] == colors[i + 1 ]) {
15
+ check = false ;
16
+ break ;
17
+ }
18
+ }
19
+
20
+ if (check) {
21
+ ++count;
22
+ }
23
+
24
+
25
+ for (int i = 1 ; i < n; ++i) {
26
+
27
+ int last = i - 1 ;
28
+ int first = (i + k - 1 ) % n;
29
+ if (check) {
30
+ if (colors[last] == colors[(last + 1 ) % n] || colors[first] == colors[(first - 1 + n) % n]) {
31
+ check = false ;
32
+ }
33
+ } else {
34
+ check = true ;
35
+ for (int j = i; j < i + k - 1 ; ++j) {
36
+ if (colors[j % n] == colors[(j + 1 ) % n]) {
37
+ check = false ;
38
+ break ;
39
+ }
40
+ }
41
+ }
42
+
43
+ if (check) {
44
+ ++count;
45
+ }
46
+ }
47
+
48
+ return count;
49
+ }
50
+ };
You can’t perform that action at this time.
0 commit comments