其他分享
首页 > 其他分享> > 1574. Shortest Subarray to be Removed to Make Array Sorted

1574. Shortest Subarray to be Removed to Make Array Sorted

作者:互联网

Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing.

A subarray is a contiguous subsequence of the array.

Return the length of the shortest subarray to remove.

 

Example 1:

Input: arr = [1,2,3,10,4,2,3,5]
Output: 3
Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
Another correct solution is to remove the subarray [3,10,4].

Example 2:

Input: arr = [5,4,3,2,1]
Output: 4
Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].

Example 3:

Input: arr = [1,2,3]
Output: 0
Explanation: The array is already non-decreasing. We do not need to remove any elements.

Scan from left to right, find the first index left that A[left] > A[left + 1].

 

If left == N - 1, this array is already non-descending, return 0.

 

Scan from right to left, find the first index right that A[right] < A[right - 1].

 

image

 

Now we loosely have two options, either deleting the left-side right nodes, or deleting the right-side N - left - 1 nodes.
image

 

image

 

So the answer is at most min(N - left - 1, right).

 

Now we can use a sliding window / two pointers to get tighter result.

 

Let i = 0, j = right. And we examine if we can delete elements between i and j (exclusive) by comparing A[i] and A[j].

 

image

 

Case 1: A[j] >= A[i], we can delete elements inbetween, so we can try to update the answer using j - i - 1 and increment i to tighten the window.

 

image

 

Case 2: A[j] < A[i], we can't delete elements inbetween, so we increment j to loosen the window.

 

image

 

We loop until i > left or j == N. And the answer we get should be the minimal possible solution.

 

 1 class Solution {
 2     public int findLengthOfShortestSubarray(int[] arr) {
 3         int left = 0;
 4         while (left + 1 < arr.length && arr[left] <= arr[left + 1]) {
 5             left++;
 6         }
 7         if (left == arr.length - 1) {
 8             return 0;
 9         }
10 
11         int right = arr.length - 1;
12         while (right > left && arr[right - 1] <= arr[right]) {
13             right--;
14         }
15         int result = Math.min(arr.length - left - 1, right);
16 
17         int i = 0;
18         int j = right;
19         while (i <= left && j < arr.length) {
20             if (arr[j] >= arr[i]) {
21                 result = Math.min(result, j - i - 1);
22                 i++;
23             } else {
24                 j++;
25             }
26         }
27         return result;
28     }
29 }

 https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/830480/C%2B%2B-O(N)-Sliding-window-Explanation-with-Illustrations

标签:arr,right,Make,remove,Subarray,Sorted,array,subarray,left
来源: https://www.cnblogs.com/beiyeqingteng/p/14531251.html