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 alli % 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 eithernums[0]
ornums[1]
to makenums
= [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to makenums
beautiful.
Example 2:
Input: nums = [1,1,2,2,3,3] Output: 2 Explanation: You can deletenums[0]
andnums[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:
- Minimum Deletions to Make Character Frequencies Unique (Medium)
- Minimum Operations to Make the Array Alternating (Medium)
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-indexedA[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]
toA[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/
// Author: github.com/lzl124631x
// 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;
}
};
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/
// Author: github.com/lzl124631x
// 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;
}
};
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1886930