3208. Alternating Groups II
Difficulty: Medium
Topics: Array
, Sliding Window
There is a circle of red and blue tiles. You are given an array of integers colors
and an integer k
. The color of tile i
is represented by colors[i]
:
-
colors[i] == 0
means that tilei
is red. -
colors[i] == 1
means that tilei
is blue.
An alternating group is every k
contiguous tiles in the circle with alternating colors (each tile in the group except the first and last one has a different color from its left and right tiles).
Return the number of alternating groups.
Note that since colors
represents a circle, the first and the last tiles are considered to be next to each other.
Example 1:
- Input: colors = [0,1,0,1,0], k = 3
- Output: 3
- Explanation:
- Alternating groups:
Example 2:
- Input: colors = [0,1,0,0,1,0,1], k = 6
- Output: 2
- Explanation:
- Alternating groups:
Example 3:
- Input: colors = [1,1,0,1], k = 4
- Output: 0
- Explanation:
Constraints:
3 <= colors.length <= 105
0 <= colors[i] <= 1
3 <= k <= colors.length
Hint:
- Try to find a tile that has the same color as its next tile (if it exists).
- Then try to find maximal alternating groups by starting a single for loop from that tile.
Solution:
We need to count the number of alternating groups of size k
in a circular array of colors. An alternating group is defined as a sequence of k
contiguous tiles where each tile (except the first and last) has a different color from both its left and right neighbors.
Approach
-
Valid Array Construction: First, we construct a
valid
array where each element is1
if the corresponding consecutive elements in the originalcolors
array are different, and0
otherwise. This helps us identify positions where consecutive elements do not form an alternating pattern. -
Check All Valid: If all elements in the
valid
array are1
, it means the entire array is already alternating, and every possible window of sizek
is valid. In this case, the answer is simply the length of the array. -
Run Length Encoding: If there are invalid segments (indicated by
0
in thevalid
array), we split thevalid
array into runs of consecutive1
s and0
s. This helps us identify valid segments where consecutive elements alternate. -
Circular Array Handling: Since the array is circular, we need to handle the case where the first and last runs of
1
s can be merged if they are adjacent in the circular arrangement. -
Count Valid Windows: For each run of
1
s, calculate the number of valid windows of sizek-1
(since each window of sizek
in the original array corresponds to a window of sizek-1
in thevalid
array).
Let's implement this solution in PHP: 3208. Alternating Groups II
<?php
/**
* @param Integer[] $colors
* @param Integer $k
* @return Integer
*/
function numberOfAlternatingGroups($colors, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo numberOfAlternatingGroups([0,1,0,1,0], 3) . "\n"; // Output: 3
echo numberOfAlternatingGroups([0,1,0,0,1,0,1], 6) . "\n"; // Output: 2
echo numberOfAlternatingGroups([1,1,0,1], 4) . "\n"; // Output: 0
?>
Explanation:
-
Valid Array Construction: The
valid
array is constructed to mark positions where consecutive elements in the original array are different, which helps in quickly identifying valid segments. -
Check All Valid: If all elements in the
valid
array are1
, every possible window of sizek
is valid, and the result is the length of the array. - Run Length Encoding: This step identifies contiguous segments of valid and invalid positions, allowing efficient processing of valid segments.
- Circular Array Handling: Merging the first and last valid segments if they wrap around the array ensures correct handling of circularity.
-
Count Valid Windows: For each valid segment, the number of valid windows of size
k
is calculated based on the segment length, ensuring efficient computation using sliding window principles.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)