A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1574. Shortest Subarray to be Removed to Make Array Sorted, the solution in this repository is mainly a binary search solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: binary search, two pointers, sliding window.
The notes already sitting in the source point us in the right direction:
- Two pointer
- https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/830416/Java-Increasing-From-Left-Right-and-Merge-O(n)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findLengthOfShortestSubarray.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Two pointer
02//https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/830416/Java-Increasing-From-Left-Right-and-Merge-O(n)
03//Runtime: 276 ms, faster than 100.00% of C++ online submissions for Shortest Subarray to be Removed to Make Array Sorted.
04//Memory Usage: 66.9 MB, less than 75.00% of C++ online submissions for Shortest Subarray to be Removed to Make Array Sorted.
05class Solution {
06public:
07 int findLengthOfShortestSubarray(vector<int>& arr) {
08 int n = arr.size();
09
10 int l = 0, r = n-1;
11
12 while(l+1 < n && arr[l] <= arr[l+1]){
13 ++l;
14 }
15 //arr[0...l] is non-decreasing
16
17 //the whole array is non-decreasing
18 if(l == n-1) return 0;
19
20 while(r-1 > 0 && arr[r-1] <= arr[r]){
21 --r;
22 }
23 //arr[r...n-1] is non-decreasing
24
25 //remove [l+1...n-1] or [0...r-1]
26 int min_len = min(n-1-l, r);
27
28 /*
29 move the two pointer i and j in their valid range,
30 i.e. keep the two subarrays in both sides,
31 and remove the subarray in the middle
32 */
33 for(int i = 0, j = r; i <= l && j < n; ++i){
34 while(j < n && arr[i] > arr[j]){
35 ++j;
36 }
37
38 if(j < n){
39 //now arr[i] <= arr[j]
40 //remove [i+1...j-1]
41 min_len = min(min_len, j-1-i);
42 }
43 }
44
45 return min_len;
46 }
47};
Cost