An elegant way to solve this problem is to use a greedy approach. The goal is to minimize the number of deletions, which is equivalent to maximizing the number of elements we keep. We can build the longest possible "beautiful" subsequence by iterating through the input array nums from left to right.
Let's call the beautiful array we are constructing res. We'll process each number from nums and decide whether to add it to res.
The rules for a beautiful array depend on the new indices of the elements, specifically whether an index i is even or odd. This suggests that our decision to keep an element should depend on the current length of our res array.
Here's the greedy strategy:
- Initialize an empty list, let's call it
beautiful_seq, which will store the elements we decide to keep. - Iterate through each number
numin the input arraynums. - For each
num, check the current length ofbeautiful_seq:- If
len(beautiful_seq)is even: The next element we add will be at an even index (0,2,4, ...). This is the first element of a new pair. There are no restrictions on this element, so we can greedily addnumtobeautiful_seq. - If
len(beautiful_seq)is odd: The next element we add will be at an odd index (1,3,5, ...). This is the second element of a pair. The conditionnums[i] != nums[i + 1]applies here (for the new indices). Therefore, thenumwe are considering must be different from the last element added to our sequence (beautiful_seq[-1]).- If
num != beautiful_seq[-1], we can add it to complete the pair. - If
num == beautiful_seq[-1], we cannot add it, as it would violate the condition. We must "delete" thisnumby simply not adding it to our sequence and continuing our search for a suitable second element for the pair.
- If
- If
- After iterating through all of
nums,beautiful_seqcontains the longest possible subsequence that satisfies the conditionnums[i] != nums[i+1]for all eveni. - The final step is to ensure the length of our resulting array is even. If
len(beautiful_seq)is odd, it means we have a leftover element at the end without a pair. To make the array beautiful, this last element must also be deleted. - The minimum number of deletions is the original length of
numsminus the final length of our valid beautiful sequence.
Let's trace this with an example: nums = [1, 1, 2, 3, 5]
-
Initialize
beautiful_seq = []. -
Process
1:len(beautiful_seq)is 0 (even). Add1.beautiful_seqis now[1]. -
Process
1:len(beautiful_seq)is 1 (odd). The current number1is equal to the last elementbeautiful_seq[-1], which is1. We cannot add it. It's deleted.beautiful_seqremains[1]. -
Process
2:len(beautiful_seq)is 1 (odd). The current number2is not equal tobeautiful_seq[-1](1). Add2.beautiful_seqis now[1, 2]. -
Process
3:len(beautiful_seq)is 2 (even). Add3.beautiful_seqis now[1, 2, 3]. -
Process
5:len(beautiful_seq)is 3 (odd). The current number5is not equal tobeautiful_seq[-1](3). Add5.beautiful_seqis now[1, 2, 3, 5]. -
After the loop,
beautiful_seqis[1, 2, 3, 5]. Its length is 4. -
Since the length (4) is even, this is a valid beautiful array.
-
The number of elements we kept is 4. The original length was 5.
-
Minimum deletions =
5 - 4 = 1.
Here is the implementation of this logic in Python:
from typing import List
class Solution:
def minDeletion(self, nums: List[int]) -> int:
# An empty array is already beautiful, 0 deletions needed.
if not nums:
return 0
# This list will store the elements of the longest beautiful subsequence.
beautiful_seq = []
for num in nums:
# If the length of our beautiful sequence is even, the next element
# will be at an even index (0, 2, 4...). This is the first
# element of a pair, so there are no restrictions. We add it.
if len(beautiful_seq) % 2 == 0:
beautiful_seq.append(num)
# If the length is odd, the next element will be at an odd index
# (1, 3, 5...). This is the second element of a pair.
# It must be different from the previous element to be valid.
elif num != beautiful_seq[-1]:
beautiful_seq.append(num)
# If num == beautiful_seq[-1], we must "delete" it by not adding it.
# At this point, beautiful_seq is the longest subsequence satisfying the pair condition.
# Now, we must also satisfy the even length condition.
# If the length is odd, the last element is unpaired and must be removed.
final_len = len(beautiful_seq)
if final_len % 2 != 0:
final_len -= 1
# The total number of deletions is the original length minus the
# length of the final beautiful subsequence we constructed.
return len(nums) - final_len