Formatted question description: https://leetcode.ca/all/2216.html

2216. Minimum Deletions to Make Array Beautiful (Medium)

You are given a 0-indexed integer array nums. The array nums is beautiful if:

  • nums.length is even.
  • nums[i] != nums[i + 1] for all i % 2 == 0.

Note that an empty array is considered beautiful.

You can delete any number of elements from nums. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged.

Return the minimum number of elements to delete from nums to make it beautiful.

 

Example 1:

Input: nums = [1,1,2,3,5]
Output: 1
Explanation: You can delete either nums[0] or nums[1] to make nums = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.

Example 2:

Input: nums = [1,1,2,2,3,3]
Output: 2
Explanation: You can delete nums[0] and nums[5] to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105

Companies:
Microsoft

Related Topics:
Dynamic Programming, Greedy

Similar Questions:

Solution 1. Two Pointers + Greedy

We can greedily find the even and odd pairs from left to right.

Why greedy? Think if the first two elements, if they are equal, we have to delete one of them – deleting the rest of the numbers won’t change anything to these first two equal numbers. So, when we see two equal numbers, we have to delete one of them, until we find two non-equal numbers.

Use two pointers i the write pointer and j the read pointer.

We keep the following process until j exhausts the array.

  • Write A[j] to the even-indexed A[i] and increment both pointers.
  • Try to find the odd-indexed value. Keep skipping/deleting if A[j] == A[i-1].
  • If found a valid odd-indexed value, write A[j] to A[i] and increment both pointers.

Lastly, if the resultant array has an odd length (i.e. i % 2 != 0), we delete the last element.

// OJ: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/
// Time: O(N)
// Space: O(1) extra space
class Solution {
public:
    int minDeletion(vector<int>& A) {
        int i = 0, N = A.size(), j = 0, ans = 0;
        while (j < N) {
            A[i++] = A[j++]; // Write `A[j]` to the even-indexed `A[i]`, increment both pointers
            while (j < N && A[i - 1] == A[j]) ++j, ++ans; // Trying to find the odd-indexed value. Keep skipping/deleting if `A[j] == A[i-1]`
            if (j < N) A[i++] = A[j++]; // If found, write `A[j]` to `A[i]` and increment both pointers
        }
        if (i % 2) ++ans; // If the resultant array is of odd length, delete the last element
        return ans;
    }
};

Solution 2. Greedy

Similar to Solution 1, just find non-equal pairs A[i] and A[i + 1]. If A[i] == A[i + 1], delete A[i] and ++i.

If in the end, there is no such pair, delete A[i].

// OJ: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/
// Time: O(N)
// Space: O(1)
class Solution {
public:
    int minDeletion(vector<int>& A) {
        int i = 0, N = A.size(), ans = 0;
        for (; i < N; i += 2) {
            while (i + 1 < N && A[i] == A[i + 1]) ++i, ++ans; // delete `A[i]`
            if (i + 1 == N) ++ans; // can't find a pair, delete `A[i]`
        }
        return ans;
    }
};

Discuss

https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1886930

All Problems

All Solutions