classSolution{public:intminDeletion(vector<int>&nums){intans=0;for(inti=0;i+1<nums.size();++i)// i - ans := the index after deletionif(nums[i]==nums[i+1]&&(i-ans)%2==0)++ans;returnans+((nums.size()-ans)&1);}};
1 2 3 4 5 6 7 8 9101112
classSolution{publicintminDeletion(int[]nums){intans=0;for(inti=0;i+1<nums.length;++i)// i - ans := the index after deletionif(nums[i]==nums[i+1]&&(i-ans)%2==0)++ans;returnans+(((nums.length-ans)&1)==1?1:0);}}
1 2 3 4 5 6 7 8 91011
classSolution:defminDeletion(self,nums:list[int])->int:ans=0foriinrange(len(nums)-1):# i - ans := the index after deletionifnums[i]==nums[i+1]and(i-ans)%2==0:ans+=1# Add one if the length after deletion is oddreturnans+((len(nums)-ans)&1)