Skip to main content

3206. Alternating Groups I

class Solution {
public:
int numberOfAlternatingGroups(vector<int>& colors)
{
int n = colors.size();
for (int i = 0; i < n; ++i)
{
colors.push_back(colors[i]);
}

int cnt = 0;
for (int i = 0; i < n; ++i)
{
if (colors[i] == colors[i + 2] && colors[i] != colors[i + 1])
{
++cnt;
}
}
return cnt;

}
};
  • T: O(n)O(n)
  • S: O(1)O(1)