Sorting Three Groups - Problem

You are given an integer array nums where each element is exactly 1, 2, or 3. Your goal is to make the array non-decreasing (sorted in ascending order) by performing the minimum number of removal operations.

In each operation, you can remove any element from the array. The challenge is to find the minimum number of elements to remove so that the remaining elements form a non-decreasing sequence.

Example: If nums = [2,1,3,2,1], you could remove the elements at indices 1 and 4 (both 1's) to get [2,3,2], then remove one more 2 to get [2,3] which is non-decreasing. The answer would be 3 operations.

Key insight: This is equivalent to finding the longest non-decreasing subsequence and subtracting its length from the total array length!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,1,3,2,1]
โ€บ Output: 3
๐Ÿ’ก Note: One optimal solution is to remove elements at indices 1 and 4 (both 1's) and the element at index 3 (one of the 2's), leaving [2,3] which is non-decreasing. We need 3 operations total.
example_2.py โ€” Already Sorted
$ Input: nums = [1,2,3,3,3]
โ€บ Output: 0
๐Ÿ’ก Note: The array is already non-decreasing, so no operations are needed. The minimum number of operations is 0.
example_3.py โ€” Reverse Sorted
$ Input: nums = [3,3,2,1,1]
โ€บ Output: 3
๐Ÿ’ก Note: We can keep [3,3] (removing 2,1,1) or keep [2] (removing 3,3,1,1) or keep [1,1] (removing 3,3,2). The longest non-decreasing subsequence has length 2, so we need 5-2=3 operations.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 3
  • Each element is guaranteed to be 1, 2, or 3

Visualization

Tap to expand
Library Shelf OrganizationInitial Arrangement: [2, 1, 3, 2, 1]Inter.(2)Begin.(1)Adv.(3)Inter.(2)Begin.(1)Optimal Non-Decreasing Arrangement:RemoveInter.(2)Adv.(3)RemoveRemoveโœ… Keep sequence [2, 3] - Length = 2โŒ Remove 3 books - Operations = 5 - 2 = 3Algorithm Insight:1. Track longest sequence endingwith difficulty 1, 2, or 32. For each book, extend the bestcompatible sequence3. Difficulty d can extend anysequence ending โ‰ค d4. Answer = n - max(sequence)โšก O(n) time, O(1) space!
Understanding the Visualization
1
Initial Shelf
Books are randomly arranged: [Intermediate, Beginner, Advanced, Intermediate, Beginner]
2
Track Best Sequences
For each difficulty level, track the longest valid sequence ending at that level
3
Update Optimally
When placing a book, extend the best possible sequence from equal or lower difficulties
4
Calculate Removals
Remove minimum books = Total books - Longest valid sequence
Key Takeaway
๐ŸŽฏ Key Insight: Since we only have 3 possible values, we can efficiently track the optimal subsequence ending at each value in O(n) time rather than using the traditional O(nยฒ) LIS approach!
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~12 min Avg. Time
892 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