Shortest Subarray to be Removed to Make Array Sorted
Given an integer array arr, remove a subarray (which can be empty) from arr such that the remaining elements in arr are non-decreasing.
Return the length of the shortest subarray to remove.
A subarray is a contiguous subsequence of the array.
Example 1
Input
arr = [1,2,3,10,4,2,3,5]Output
3The shortest subarray we can remove is [10,4,2] of length 3, leaving [1,2,3,3,5] sorted; another correct solution is to remove [3,10,4].
Example 2
Input
arr = [5,4,3,2,1]Output
4Since the array is strictly decreasing, we can only keep a single element, so we need to remove a subarray of length 4.
Constraints
- 1 <= arr.length <= 10^5
- 0 <= arr[i] <= 10^9