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 eithernums[0]ornums[1]to makenums= [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to makenumsbeautiful.
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 <= 1050 <= nums[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to making an array beautiful is to try every single possible combination of deletions. We will check each of these combinations to find the one that requires the fewest deletions and also makes the array beautiful.
Here's how the algorithm would work step-by-step:
def min_deletions_brute_force(numbers):
min_deletions = float('inf')
array_length = len(numbers)
for i in range(2**array_length):
subset = []
deletions = 0
for j in range(array_length):
if (i >> j) & 1:
subset.append(numbers[j])
else:
deletions += 1
is_beautiful = True
subset_length = len(subset)
# Check if the subset is beautiful
for k in range(0, subset_length - 1, 2):
if subset[k] == subset[k+1]:
is_beautiful = False
break
#If the subset has an odd number of elements, it can still be beautiful
if subset_length % 2 != 0:
pass
elif is_beautiful:
min_deletions = min(min_deletions, deletions)
return min_deletionsThe goal is to make the arrangement 'beautiful' by strategically removing as few items as possible. We look at the items one by one and decide whether to keep it or discard it to maintain an alternating pattern where neighboring items are different.
Here's how the algorithm would work step-by-step:
def min_deletion(numbers):
deletion_count = 0
items_kept_count = 0
last_kept_number = -1
for number in numbers:
# When even items are kept, current item must differ.
if items_kept_count % 2 == 0:
if number == last_kept_number:
deletion_count += 1
# Keep the current number and update last_kept_number.
else:
items_kept_count += 1
last_kept_number = number
# If an odd number of items are kept, we keep the number.
else:
items_kept_count += 1
last_kept_number = number
# If the final length is odd, we need to delete one more.
if items_kept_count % 2 != 0:
deletion_count += 1
return deletion_count| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0 since an empty array is considered beautiful by default. |
| Array with a single element | Return 1, as the array must have an even length to be beautiful, so we delete the single element. |
| Array with two identical elements | Return 1, as deleting either element makes the array beautiful. |
| Array with an odd number of elements where the last two elements are equal after deletions. | Account for the final element if, after deletions, the effective length is still odd; delete the last element to satisfy the even length constraint. |
| Array with all identical elements | The number of deletions will be the smallest number to make the array beautiful. |
| Large input array size | Ensure the solution has linear time complexity, such as iterating through the array once. |
| Array with a long sequence of identical elements at the end | The algorithm should correctly handle the sequence of identical elements and make necessary deletions to maintain the beautiful property. |
| Integer overflow if calculating array size or index | Use appropriate data types (e.g., long in Java or C++) to prevent integer overflow issues when dealing with large array sizes or indices. |