Using Two pointers Beats 94.71% Python3
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires ensuring that no two consecutive elements in an array are the same
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires ensuring that no two consecutive elements in an array are the same, and the array length is even. The simplest approach is to iterate from the left, removing duplicates when they appear next to each other. By carefully tracking deletions, we can ensure that the resulting array meets the required conditions.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Iterate from the left: We assume that each step leaves the left portion of the array valid, and we only delete elements when necessary to avoid duplicates.\n- Use two pointers: One pointer (current_ptr) tracks the current valid element, and the other (ptr) checks the next element. If the two elements are the same, we increment the count_to_delete and move the ptr forward.\n- Handle array length: After processing the array, we check if the number of deletions leads to an odd-length array. If so, we make one additional deletion to ensure the length is even.\n\n# Complexity\n- Time complexity: O(n) -> n is the length of the list\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 minDeletion(self, nums: List[int]) -> int:\n        """\n        The length of the array should be even\n        and (so if it have even length I can only delete even elements)\n        if it have odd (length I can only delete odd length)\n        """\n        n = len(nums)\n        count_to_delete = 0\n\n        # pointers to track the non-deleted\n        ptr = 1\n        current_ptr = 0\n        \n        while ptr < n:\n            if nums[current_ptr] == nums[ptr]:\n                ptr += 1\n                count_to_delete += 1\n            else:\n                current_ptr = ptr + 1\n                ptr += 2\n\n        if n % 2 != count_to_delete % 2:\n            count_to_delete += 1\n        \n        return count_to_delete\n```
['Two Pointers', 'Greedy', 'Python3']