Welcome to Subscribe On Youtube
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 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)
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-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/ // 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; } };
-
class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) i = ans = 0 while i < n - 1: if nums[i] == nums[i + 1]: ans += 1 i += 1 else: i += 2 if (n - ans) % 2: ans += 1 return ans ############ # 2216. Minimum Deletions to Make Array Beautiful # https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/ class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) prev = None valid = 0 for x in nums: if prev is not None: if x != prev: prev = None valid += 2 else: prev = x return n - valid
-
class Solution { public int minDeletion(int[] nums) { int n = nums.length; int ans = 0; for (int i = 0; i < n - 1; ++i) { if (nums[i] == nums[i + 1]) { ++ans; } else { ++i; } } if ((n - ans) % 2 == 1) { ++ans; } return ans; } }
-
func minDeletion(nums []int) int { n := len(nums) ans := 0 for i := 0; i < n-1; i++ { if nums[i] == nums[i+1] { ans++ } else { i++ } } if (n-ans)%2 == 1 { ans++ } return ans }
-
function minDeletion(nums: number[]): number { const n = nums.length; let res = 0; let i = 0; while (i < n - 1) { if (nums[i] === nums[i + 1]) { i++; res++; } else { i += 2; } } if ((n - res) % 2 === 1) { res++; } return res; }
-
impl Solution { pub fn min_deletion(nums: Vec<i32>) -> i32 { let n = nums.len(); let mut res = 0; let mut i = 0; while i < n - 1 { if nums[i] == nums[i + 1] { res += 1; i += 1; } else { i += 2; } } if (n - res) % 2 == 1 { res += 1; } res as i32 } }
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; } };
-
class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) i = ans = 0 while i < n - 1: if nums[i] == nums[i + 1]: ans += 1 i += 1 else: i += 2 if (n - ans) % 2: ans += 1 return ans ############ # 2216. Minimum Deletions to Make Array Beautiful # https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/ class Solution: def minDeletion(self, nums: List[int]) -> int: n = len(nums) prev = None valid = 0 for x in nums: if prev is not None: if x != prev: prev = None valid += 2 else: prev = x return n - valid
-
class Solution { public int minDeletion(int[] nums) { int n = nums.length; int ans = 0; for (int i = 0; i < n - 1; ++i) { if (nums[i] == nums[i + 1]) { ++ans; } else { ++i; } } if ((n - ans) % 2 == 1) { ++ans; } return ans; } }
-
func minDeletion(nums []int) int { n := len(nums) ans := 0 for i := 0; i < n-1; i++ { if nums[i] == nums[i+1] { ans++ } else { i++ } } if (n-ans)%2 == 1 { ans++ } return ans }
-
function minDeletion(nums: number[]): number { const n = nums.length; let res = 0; let i = 0; while (i < n - 1) { if (nums[i] === nums[i + 1]) { i++; res++; } else { i += 2; } } if ((n - res) % 2 === 1) { res++; } return res; }
-
impl Solution { pub fn min_deletion(nums: Vec<i32>) -> i32 { let n = nums.len(); let mut res = 0; let mut i = 0; while i < n - 1 { if nums[i] == nums[i + 1] { res += 1; i += 1; } else { i += 2; } } if (n - res) % 2 == 1 { res += 1; } res as i32 } }
Discuss
https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/discuss/1886930