Very simple python array based approach in 22 lines
Intuition\nMy goal was to solve this as simply as possible in a pythonic way. Storing the groups in arrays and computing the boundaries of the groups is quite s
# Intuition\nMy goal was to solve this as simply as possible in a pythonic way. Storing the groups in arrays and computing the boundaries of the groups is quite simple with numpy arrays. The goal was to be "fast enough" and as simple as possible.\n\n# Approach\nStore groups in boolean numpy arrays and compute the lengths of the groups with simple array operations. This approach could be made much faster by doing some kind of smart updae on the ranges representing the groups. But for that kind of extra effort, it would probably be better to use a tree based approach, and for a tree based approach, python is going to be real slow.\n\n# Complexity\n- Time complexity:\n    O(Q*N)\n- Space complexity:\n    O(N)\n\n\n# Code\n```\nimport numpy as np\nclass Solution:\n    def numberOfAlternatingGroups(self, colors, queries):\n        N = len(colors)\n        colors = np.tile(np.array(colors, dtype=bool), 2)  # wrap around\n        blocks = (colors[1:] != colors[:-1])  # runs of True will be "groups," but ending early by 1\n        result = list()\n        for query in queries:\n            if query[0] == 1:\n                size = query[1]\n                blkcrop = blocks[:N + size - 1]  # crop to appropriate wrap around\n                diff = (blkcrop[1:N + size - 1] != blocks[:N + size - 2])\n                breaks = np.nonzero(diff)[0] + 1  # index of group beg/eng\n                if blkcrop[+0]: breaks = np.concatenate([[0], breaks])\n                if blkcrop[-1]: breaks = np.concatenate([breaks, [N + size - 2]])\n                lens = breaks[1::2] - breaks[0::2] + 1  # lengths of the groups; +1 b/c they end early\n                count = np.sum(np.maximum(0, lens - size + 1))  # size that can be packed into groups\n                result.append(count)\n            else:\n                colors[query[1]:None:N] = query[2]\n                blocks = (colors[1:] != colors[:-1])  # the simple update is only slightly slower\n        return result\n\n      \n```
['Python3']