We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Minimum Number of Increasing Subsequence to Be Removed

Number: 3545

Difficulty: Hard

Paid? Yes

Companies: N/A


Problem Description

Given an array of integers, you are allowed to repeatedly remove any strictly increasing subsequence until the array becomes empty. The goal is to determine the minimum number of removal operations required to completely empty the array.


Key Insights

  • The process of removing strictly increasing subsequences until the array is empty is equivalent to partitioning the array into strictly increasing subsequences.
  • A classic combinatorial result (via Dilworth’s theorem) shows that the minimum number of strictly increasing subsequences needed to cover the entire array is equal to the length of the longest non‐increasing subsequence.
  • Therefore, instead of simulating the removal process, we can solve the problem by finding the length of the longest non-increasing subsequence.
  • Due to the problem constraints (up to 10^5 elements), an O(n log n) algorithm is necessary, which can be achieved using a binary search–based method.

Space and Time Complexity

Time Complexity: O(n log n) – Each of the n elements is processed with a binary search over a list whose length is at most n. Space Complexity: O(n) – The extra space used to maintain the dynamic structure (or "dp" array) for binary search.


Solution

We solve the problem by noticing that the minimum number of strictly increasing subsequences needed to remove the entire array equals the length of the longest non-increasing subsequence (LNIS). To compute the LNIS efficiently, we transform the problem into finding the longest non-decreasing subsequence (LNDS) of an array where each element is negated.

Steps:

  1. Negate every element of the input array. In the resulting array, a non-decreasing subsequence corresponds to a non-increasing subsequence in the original array.
  2. Use a modified patience sorting algorithm (with binary search) to compute the longest non-decreasing subsequence in the negated array. For handling equal values properly (since non-decreasing allows equality), use an upper-bound binary search (i.e. bisect_right in Python) to determine the insertion point.
  3. The length of the computed subsequence is the answer.

Key Data Structures and Techniques:

  • Array transformation (negation).
  • Dynamic list (often called tails or dp) to store the minimum possible tail for each subsequence length.
  • Binary search to efficiently update the dp list while iterating over the transformed array.

Code Solutions

Below are implementations in Python, JavaScript, C++, and Java with detailed line-by-line comments.

import bisect

def min_operations_to_remove(nums):
    # Negate the numbers to transform longest non-increasing subsequence problem
    # into longest non-decreasing subsequence problem.
    neg_nums = [-num for num in nums]
    
    # dp will store the current minimal tail values for all non-decreasing subsequences
    dp = []
    
    for num in neg_nums:
        # For non-decreasing sequence, we use bisect_right which returns the insertion point 
        # to the right of any existing entries equal to num.
        pos = bisect.bisect_right(dp, num)
        
        # If pos equals the length of dp, then num extends the longest subsequence so far.
        if pos == len(dp):
            dp.append(num)
        else:
            # Otherwise, we update dp at the found position with num to potentially form a
            # new subsequence that may allow for further extension.
            dp[pos] = num
    # The length of dp corresponds to the length of the longest non-increasing subsequence
    # in the original array.
    return len(dp)

# Example usage:
nums1 = [5,3,1,4,2]
print(min_operations_to_remove(nums1))  # Expected output: 3

nums2 = [1,2,3,4,5]
print(min_operations_to_remove(nums2))  # Expected output: 1

nums3 = [5,4,3,2,1]
print(min_operations_to_remove(nums3))  # Expected output: 5
← Back to All Questions