class Solution: def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]: colors = [0] * n res = [0] * len(queries) pairs = 0 for idx, q in enumerate(queries): i, new_color = q left, right = i - 1, i + 1 old_color = colors[i] if old_color != 0: if left >= 0 and colors[left] == old_color: pairs -= 1 if right < n and colors[right] == old_color: pairs -= 1 if left >= 0 and colors[left] == new_color: pairs += 1 if right < n and colors[right] == new_color: pairs += 1 colors[i] = new_color res[idx] = pairs return res