---
# System prepended metadata

title: 2216. Minimum Deletions to Make Array Beautiful
tags: [Leetcode, Medium, Greedy]

---

# 2216. Minimum Deletions to Make Array Beautiful
###### tags: `Leetcode` `Medium` `Greedy`
Link: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/
## 思路
pre记录之前出现在目前array(前面可能有些element已经被删掉了)偶数位的值
```i-ans```就是每一个旧array的值在目前array的位置
所以如果
- ```i-ans```是偶数就说明它需要检查它后面的一个数是不是奇数，需要更新pre
- ```i-ans```是奇数就说明它需要检查它跟pre是否一样
## Code $O(N)$ $O(1)$
```java=
class Solution {
    public int minDeletion(int[] nums) {
        int pre = -1, ans = 0;
        for(int i=0; i<nums.length; i++){
            if((i-ans)%2==1 && nums[i]==pre) ans++;
            else if((i-ans)%2==0) pre = nums[i];
        }
        return (nums.length-ans)%2==0?ans:ans+1;
    }
}
```