Sorting Three Groups - Complete Solution Guide
Sorting Three Groups is LeetCode problem 2826, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array nums . Each element in nums is 1, 2 or 3. In each operation, you can remove an element from nums . Return the minimum number of operations to make nums non-decreasing . Example 1: Input: nums = [2,1,3,2,1] Output: 3 Explanation: One of the optimal solutions is to remove nums[0] , nums[2] and nums[3] . Example 2: Input: nums = [1,3,2,1,3,3] Output: 2 Explanation: One of the optimal solutions is to remove nums[1] and nums[2] . Example 3: Input: nums = [2,2,2,2,3,3] O
Detailed Explanation
The problem asks us to find the minimum number of operations (removals) needed to make a given array `nums` non-decreasing. The array `nums` contains only the numbers 1, 2, and 3. A non-decreasing array means that each element is greater than or equal to the element before it. The input is an integer array `nums`, and the output is the minimum number of removals needed to make the array non-decreasing.
Solution Approach
The solution uses dynamic programming to find the length of the longest non-decreasing subsequence of the input array. It maintains three variables: `dp1`, `dp2`, and `dp3`. `dp1` stores the length of the longest non-decreasing subsequence ending with 1, `dp2` stores the length of the longest non-decreasing subsequence ending with 2, and `dp3` stores the length of the longest non-decreasing subsequence ending with 3. For each element `x` in the input array, we update the values of `dp1`, `dp2`, and `dp3` based on the value of `x`. Finally, the maximum of `dp1`, `dp2`, and `dp3` gives the length of the longest non-decreasing subsequence, and the minimum number of removals is the length of the input array minus this value.
Step-by-Step Algorithm
- Step 1: Initialize `dp1`, `dp2`, and `dp3` to 0. These variables will store the lengths of the longest non-decreasing subsequences ending with 1, 2, and 3, respectively.
- Step 2: Iterate through the input array `nums`. For each element `x`:
- Step 3: If `x` is 1, increment `dp1` by 1.
- Step 4: If `x` is 2, update `dp2` to `max(dp1, dp2) + 1`. This is because if we append 2 to a subsequence, we can either append it to the longest subsequence ending with 1, or the longest subsequence ending with 2.
- Step 5: If `x` is 3, update `dp3` to `max(dp1, dp2, dp3) + 1`. This is because if we append 3 to a subsequence, we can either append it to the longest subsequence ending with 1, 2 or 3.
- Step 6: After iterating through the entire array, find the maximum of `dp1`, `dp2`, and `dp3`. This represents the length of the longest non-decreasing subsequence.
- Step 7: Return the length of the input array minus the maximum of `dp1`, `dp2`, and `dp3`. This is the minimum number of removals needed to make the array non-decreasing.
Key Insights
- Insight 1: The problem can be solved by finding the longest non-decreasing subsequence of the input array. The minimum number of removals is then the length of the input array minus the length of the longest non-decreasing subsequence.
- Insight 2: Dynamic programming can be used to efficiently calculate the length of the longest non-decreasing subsequence. We only need to keep track of the longest non-decreasing subsequence ending with 1, 2, or 3.
- Insight 3: Since the input array only contains 1, 2, and 3, we can use a constant amount of space to store the lengths of the longest non-decreasing subsequences.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Binary Search, Dynamic Programming.
Companies
Asked at: UiPath.