Count Alternating Tile Groups
Learn this problemProblem statement
Imagine there is a circle of red and blue tiles. The color of the tiles are represented by the array tileColors, where tileColors[i] = 0 means that the ith tile is red, whereas tileColors[i] = 1 means that the ith tile is blue.
We want to determine whether the tiles that are next to each other in the circle have alternating colors. The ith tile should have a different color than both the i+1th and the i-1th neighboring tiles. Given an integer size, determine how many groups of size consecutive tiles have alternating colors.
Note: Because tileColors represents a circle, the first and last tiles are considered to be next to each other.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(tileColors.length2) will fit within the execution time limit.
Function
solution(tileColors: int[], size: int) → intExamples
Example 1
tileColors = [0, 1, 0, 1, 1]size = 3return = 3There are five unique groups of size 3:
tileColors[0...2] = [0, 1, 0], which has alternating colors.tileColors[1...3] = [1, 0, 1], which has alternating colors.tileColors[2...4] = [0, 1, 1], which does not have alternating colors.tileColors[3...4] + tileColors[0] = [1, 1, 0], which does not have alternating colors.tileColors[4] + tileColors[0...1] = [1, 0, 1], which has alternating colors.
There are 3 groups that have alternating colors.
Constraints
- Each element of
tileColorsis either0(red) or1(blue). tileColorscontains at least1element.1 <= size <= tileColors.length- A solution with time complexity
O(tileColors.length2)or better is sufficient.