-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (28 loc) · 743 Bytes
/
Solution.cs
File metadata and controls
29 lines (28 loc) · 743 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
public class Solution
{
public int NumberOfAlternatingGroups(int[] colors, int k)
{
int n = colors.Length;
int[] newColors = new int[n + k];
for (int i = 0; i < n + k; i++)
{
newColors[i] = colors[i % n];
}
int count = 0;
int left = 0, right = 0;
while (right < n + k)
{
if (right - left < k - 1)
{
int prev = right;
right++;
if (right == n + k - 1) break;
if (newColors[right] == newColors[prev]) left = prev + 1;
continue;
}
if (right - left == k - 1) count++;
left++;
}
return count;
}
}