Minimum Number of Increasing Subsequence to Be Removed - Problem

You're given an array of integers, and your task is to completely empty it using a special operation: removing strictly increasing subsequences.

A strictly increasing subsequence is a sequence where each element is greater than the previous one. For example, in array [3, 1, 4, 1, 5], we could remove subsequence [1, 4, 5] or [3, 4, 5].

Goal: Find the minimum number of operations needed to make the array completely empty.

Key insight: This problem is equivalent to finding the length of the longest non-increasing subsequence, because elements in a non-increasing subsequence must be removed in separate operations.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 3, 6, 7, 9, 4, 10, 5, 6]
โ€บ Output: 3
๐Ÿ’ก Note: We can remove three increasing subsequences: [1, 3, 6, 7, 9, 10], [4, 5, 6], and the array becomes empty. This is optimal because we need at least 3 operations due to the decreasing pattern 9โ†’4โ†’5.
example_2.py โ€” Decreasing Array
$ Input: [5, 4, 3, 2, 1]
โ€บ Output: 5
๐Ÿ’ก Note: Since the array is strictly decreasing, no two elements can be in the same increasing subsequence. We need 5 operations, one for each element.
example_3.py โ€” Increasing Array
$ Input: [1, 2, 3, 4, 5]
โ€บ Output: 1
๐Ÿ’ก Note: The entire array is already a strictly increasing subsequence, so we can remove all elements in a single operation.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • All elements are positive integers

Visualization

Tap to expand
Library Book Stacking VisualizationBooks to shelve:31415Trip 1 (Increasing):1โ†’4โ†’5Trip 2 (Remaining):3โ†’1Minimum Trips2Each trip must carry books in strictly increasing order by number
Understanding the Visualization
1
Setup
You have books scattered on your desk: [3, 1, 4, 1, 5]
2
First Trip Strategy
Start with book 3, then realize you can't take book 1 (not increasing)
3
Optimal Planning
Use binary search to plan: find the best 'stack' for each book
4
Final Result
Minimum 2 trips needed: [1,4,5] and [3,1] or similar groupings
Key Takeaway
๐ŸŽฏ Key Insight: The minimum number of trips equals the length of the longest non-increasing subsequence, because those books can never be carried together in the same trip!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
68.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen