Beats 92.68% Python.
Intuition\n Describe your first thoughts on how to solve this problem. \n\nImagine creating a boolean array of length n - 1, where the $i^{th}$ element is False
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nImagine creating a boolean array of length n - 1, where the $i^{th}$ element is `False` if `nums[i] != nums[i + 1]` and otherwise `True`. \n\nWe can map any subarray of nums to this new boolean array. If such a subarray (when mapped) contains at least one `True` value, then it is not a valid subarray. Therefore subarrays are valid if and only if their mapped subarray of boolean values only contains `False` values.\n\ne.g. \n`[0, 1, 1, 1] -> [False, True, True]`\n`[1, 0, 1, 0] -> [False, False, False]`\n\nSuppose we find a subarray, and map it to this boolean subarray, such that it only contains the value `False` and either side in the boolean array we find the values `True` bounding this subarray. \n\nSo we have the maximum-sized valid subarray as bounded by the `True` values. \n\nIf this is length $p$ (i.e. there are $p$ `False` values in the subarray) then there are $p + 1$ different alternating values in the equivalent subarray of the original array (that maps to this boolean subarrary).\n\nFollowing this there are $T_{p + 1} = p (p + 1) / 2$ valid subarrays in this array, where $T_j$ is the $j^{th}$ triangular number. To see this, think about the number of different ways a window of size $s \\le p + 1$ can be positioned in the subarray of size $p + 1$.\n\nAnswer: $(p + 1) - (s - 1)$, and $s$ can take the value from $1$ to $p + 1$: \n\ni.e. $p + 1, p, p - 1, ..., 1$ \n\nand then we need to sum these up to get \n\n$p * (p + 1) / 2$.\n\nSo we find all the max-size boolean subarrays containing only `False` values and bounded by `True` (or the edges of the boolean subarray) and get their length: $p_j$ for each $j$ in $m$, where $m$ is the number of subarrays.\n\nThen we simply compute $\\sum_{j}^m T_{p_{j} + 1} = \\sum_{j}^{m} p_{j} * (p_j + 1) / 2$.\n\nSince no two subarrays are contiguous (otherwise they would merge into a larger subarray) we can compute each $T_{p_{j} + 1}$ value on the fly whilst iterating through our list and reset the size of a subarray once we reach its end. \n\nThe $p_{j}$ values are captured by the `adjacent` variable in the code below. We don\'t actually form the boolean subarray (this would be $O(n)$ memory) but do so implicity with the check `val == nums[i + 1]`.\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(1)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def countAlternatingSubarrays(self, nums: List[int]) -> int:\n        \n        total = len(nums)\n        adjacent = 0\n        for i, val in enumerate(nums[:-1]):\n            \n            if val == nums[i + 1]:                \n                total += adjacent * (adjacent + 1) // 2\n                adjacent = 0\n            else:\n                adjacent += 1\n        \n        total += adjacent * (adjacent + 1) // 2\n        \n        return total\n\n\n```
['Python3']