Rust/Python two pointers solution with explanation
Intuition\n\nStandard two pointers solution. Have some value which tracks the previous number you have seen. Now move right pointer and check if the value you s
# Intuition\n\nStandard two pointers solution. Have some value which tracks the previous number you have seen. Now move right pointer and check if the value you see at this pointer is different from the previous seen. \n\nIf they are the same, you can\'t expand the interval and should update left pointer to right pointer. Update the previous value.\n\nAnd increase the result by `pos_r - pos_l + 1`\n\n# Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(1)$\n\n# Code\n```Rust []\nimpl Solution {\n  pub fn count_alternating_subarrays(nums: Vec<i32>) -> i64 {\n    let (mut res, mut pos_l, mut prev_v) = (0, 0, -1);\n    for pos_r in 0 .. nums.len() {\n      if nums[pos_r] == prev_v {\n        pos_l = pos_r;\n      }\n      prev_v = nums[pos_r];\n      res += (pos_r - pos_l + 1) as i64;\n    }\n    return res;\n  }\n}\n```\n```python []\nclass Solution:\n  def countAlternatingSubarrays(self, nums: List[int]) -> int:\n    res, pos_l, prev_c = 0, 0, -1\n    for pos_r in range(len(nums)):\n      if nums[pos_r] != prev_c:\n        prev_c = nums[pos_r]\n      else:\n        pos_l = pos_r\n\n      res += pos_r - pos_l + 1\n\n    return res\n```
['Python', 'Rust']