Skip to content

Instantly share code, notes, and snippets.

@lemanschik
Last active September 10, 2025 05:33
Show Gist options
  • Select an option

  • Save lemanschik/0c173b29ff77e4c091d86f10ce3b16f3 to your computer and use it in GitHub Desktop.

Select an option

Save lemanschik/0c173b29ff77e4c091d86f10ce3b16f3 to your computer and use it in GitHub Desktop.
Leetcode

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:

  1. Initialize an empty list, let's call it beautiful_seq, which will store the elements we decide to keep.
  2. Iterate through each number num in the input array nums.
  3. For each num, check the current length of beautiful_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 add num to beautiful_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 condition nums[i] != nums[i + 1] applies here (for the new indices). Therefore, the num we 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" this num by simply not adding it to our sequence and continuing our search for a suitable second element for the pair.
  4. After iterating through all of nums, beautiful_seq contains the longest possible subsequence that satisfies the condition nums[i] != nums[i+1] for all even i.
  5. 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.
  6. The minimum number of deletions is the original length of nums minus 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). Add 1. beautiful_seq is now [1].

  • Process 1: len(beautiful_seq) is 1 (odd). The current number 1 is equal to the last element beautiful_seq[-1], which is 1. We cannot add it. It's deleted. beautiful_seq remains [1].

  • Process 2: len(beautiful_seq) is 1 (odd). The current number 2 is not equal to beautiful_seq[-1] (1). Add 2. beautiful_seq is now [1, 2].

  • Process 3: len(beautiful_seq) is 2 (even). Add 3. beautiful_seq is now [1, 2, 3].

  • Process 5: len(beautiful_seq) is 3 (odd). The current number 5 is not equal to beautiful_seq[-1] (3). Add 5. beautiful_seq is now [1, 2, 3, 5].

  • After the loop, beautiful_seq is [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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment