Java/Python | Simple Greedy Solution Explained
Idea:\nif there are 2 consecutive elements that are equal and its a good index to delete then we delete it.\nevery step, good index to delete is:\nif we deleted
Idea:\nif there are 2 consecutive elements that are equal **and** its a good index to delete then we delete it.\nevery step, good index to delete is:\nif we deleted 0 elements, we want the **even** indices\nif we deleted 1 elements, we want the **odd** indicies\nif we deleted 2 elements, again we want the **even** indicies and so on.\nso every iteration just check if we need to delete the current index and the consecutive elements are equal.\n\nin the end dont forget to check if the length of the array is odd, meaning we need 1 more deletion to make it even.\n\n**Java code:**\n```\npublic int minDeletion(int[] nums) {\n        int n = nums.length;\n        int res = 0;\n        \n        for (int i = 0; i < n - 1; i++) {\n            boolean deleteIdx = res % 2 == i % 2;\n            if (deleteIdx && nums[i] == nums[i + 1]) {\n                res++;\n            }\n        }\n        if ((n - res) % 2 == 1) {\n            res++;\n        }\n        return res;\n    }\n```\n\n**Python code:**\n```\ndef minDeletion(self, nums: List[int]) -> int:\n\tn = len(nums)\n\tres = 0\n\n\tfor i in range(n - 1):\n\t\tdelete_idx = res % 2 == i % 2\n\t\tif delete_idx and nums[i] == nums[i + 1]:\n\t\t\tres += 1\n\n\tif (n - res) % 2 == 1:\n\t\tres += 1\n\treturn res\n```\n
['Greedy', 'Python', 'Java']