2216. Minimum Deletions to Make Array Beautiful LeetCode Solution
In this guide, you will get 2216. Minimum Deletions to Make Array Beautiful LeetCode Solution with the best time and space complexity. The solution to Minimum Deletions to Make Array Beautiful problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Deletions to Make Array Beautiful solution in C++
- Minimum Deletions to Make Array Beautiful solution in Java
- Minimum Deletions to Make Array Beautiful solution in Python
- Additional Resources
Problem Statement of Minimum Deletions to Make Array Beautiful
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2216. Minimum Deletions to Make Array Beautiful LeetCode Solution in C++
class Solution {
public:
int minDeletion(vector<int>& nums) {
int ans = 0;
for (int i = 0; i + 1 < nums.size(); ++i)
// i - ans := the index after deletion
if (nums[i] == nums[i + 1] && (i - ans) % 2 == 0)
++ans;
return ans + ((nums.size() - ans) & 1);
}
};
/* code provided by PROGIEZ */
2216. Minimum Deletions to Make Array Beautiful LeetCode Solution in Java
class Solution {
public int minDeletion(int[] nums) {
int ans = 0;
for (int i = 0; i + 1 < nums.length; ++i)
// i - ans := the index after deletion
if (nums[i] == nums[i + 1] && (i - ans) % 2 == 0)
++ans;
return ans + (((nums.length - ans) & 1) == 1 ? 1 : 0);
}
}
// code provided by PROGIEZ
2216. Minimum Deletions to Make Array Beautiful LeetCode Solution in Python
class Solution:
def minDeletion(self, nums: list[int]) -> int:
ans = 0
for i in range(len(nums) - 1):
# i - ans := the index after deletion
if nums[i] == nums[i + 1] and (i - ans) % 2 == 0:
ans += 1
# Add one if the length after deletion is odd
return ans + ((len(nums) - ans) & 1)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.