Skip to content

Practice Alternating Groups III

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3245
LeetCode Problem

Alternating Groups III

Problem Statement

The circular array colors uses 0 for red and 1 for blue. Process [1, size] by counting circular contiguous groups of that length whose neighboring tiles within the group alternate colors. Process [2, index, color] by changing that tile’s color. Groups may wrap around the array boundary. Return the results of type-1 queries in order.

Source: Alternating Groups III on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
colors = [0,1,1,0,1], queries = [[2,1,0],[1,4]]
Output
[2]

# Example 2

Input
colors = [0,0,1,0,1,1], queries = [[1,3],[2,3,0],[1,5]]
Output
[2,0]

Constraints

4 <= colors.length <= 5 * 10^4
0 <= colors[i] <= 1
1 <= queries.length <= 5 * 10^4
queries[i][0] == 1 or queries[i][0] == 2
For all i that: queries[i][0] == 1: queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1 queries[i][0] == 2: queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1
queries[i][0] == 1: queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1
queries[i][0] == 2: queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1

Related LeetCode Problems

Practice more problems similar to Alternating Groups III.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.