Alternating Groups III Problem
Description
LeetCode Problem 3245.
There are some red and blue tiles arranged circularly. You are given an array of integers colors and a 2D integers array queries.
The color of tile i is represented by colors[i]:
colors[i] == 0means that tileiis red.colors[i] == 1means that tileiis blue.
An alternating group is a contiguous subset of tiles in the circle with alternating colors (each tile in the group except the first and last one has a different color from its adjacent tiles in the group).
You have to process queries of two types:
queries[i] = [1, size_i], determine the count of alternating groups with sizesize_i.queries[i] = [2, index_i, color_i], changecolors[index_i]tocolor_i.
Return an array answer containing the results of the queries of the first type in order.
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,1,0,1], queries = [[2,1,0],[1,4]]
Output: [2]
Explanation:

First query:
Change colors[1] to 0.

Second query:
Count of the alternating groups with size 4:


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

First query:
Count of the alternating groups with size 3:


Second query: colors will not change.
Third query: There is no alternating group with size 5.
Constraints:
4 <= colors.length <= 5 * 10^40 <= colors[i] <= 11 <= queries.length <= 5 * 10^4queries[i][0] == 1orqueries[i][0] == 2- For all
ithat:queries[i][0] == 1:queries[i].length == 2,3 <= queries[i][1] <= colors.length - 1queries[i][0] == 2:queries[i].length == 3,0 <= queries[i][1] <= colors.length - 1,0 <= queries[i][2] <= 1
Solution
Approach: Fenwick Trees over Run Lengths
Call a position p a run start if colors[p] == colors[p - 1] (circularly). The circle splits into maximal alternating runs, each beginning at a run start. A run of length L contains max(L - s + 1, 0) alternating groups of size s. If there is no run start, every position starts an alternating group, so the answer is n; if there is exactly one, there is a single run of length n.
Maintain the run starts in a Fenwick tree over positions (to find the previous and next start), and two Fenwick trees indexed by run length that hold the number of runs and the sum of their lengths. A query with size s considers the runs with L >= s, and the answer is sumLength - (s - 1) * count. A colour update at index i can only change whether i and i + 1 are run starts, so it only inserts or removes those starts and adjusts the neighbouring run lengths.
Complexity: O((n + q) log n) time and O(n) space.
class BIT:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def add(self, i, v):
while i <= self.n:
self.c[i] += v
i += i & -i
def sum(self, i):
s = 0
while i > 0:
s += self.c[i]
i -= i & -i
return s
def kth(self, k):
pos = 0
step = 1 << self.n.bit_length()
while step:
nxt = pos + step
if nxt <= self.n and self.c[nxt] < k:
pos = nxt
k -= self.c[nxt]
step >>= 1
return pos + 1
class Solution:
def numberOfAlternatingGroups(self, colors: List[int], queries: List[List[int]]) -> List[int]:
n = len(colors)
pos = BIT(n)
cnt = BIT(n)
tot = BIT(n)
is_start = [False] * n
size = 0
def gap(a, b):
return (b - a) % n or n
def add_len(x, v):
cnt.add(x, v)
tot.add(x, v * x)
def neighbors(p):
total = pos.sum(n)
rank = pos.sum(p + 1)
q = pos.kth(rank) - 1 if rank > 0 else pos.kth(total) - 1
r = pos.kth(rank + 1) - 1 if rank < total else pos.kth(1) - 1
return q, r
def insert(p):
nonlocal size
if size == 0:
add_len(n, 1)
else:
q, r = neighbors(p)
add_len(gap(q, r), -1)
add_len(gap(q, p), 1)
add_len(gap(p, r), 1)
pos.add(p + 1, 1)
is_start[p] = True
size += 1
def erase(p):
nonlocal size
pos.add(p + 1, -1)
is_start[p] = False
size -= 1
if size == 0:
add_len(n, -1)
else:
q, r = neighbors(p)
add_len(gap(q, p), -1)
add_len(gap(p, r), -1)
add_len(gap(q, r), 1)
for i in range(n):
if colors[i] == colors[i - 1]:
insert(i)
ans = []
for q in queries:
if q[0] == 1:
s = q[1]
if size == 0:
ans.append(n)
else:
c = cnt.sum(n) - cnt.sum(s - 1)
t = tot.sum(n) - tot.sum(s - 1)
ans.append(t - (s - 1) * c)
else:
i, c = q[1], q[2]
if colors[i] == c:
continue
colors[i] = c
for p in (i, (i + 1) % n):
now = colors[p] == colors[p - 1]
if now != is_start[p]:
if now:
insert(p)
else:
erase(p)
return ans