java.lang.Object
g1501_1600.s1574_shortest_subarray_to_be_removed_to_make_array_sorted.Solution

public class Solution extends Object
1574 - Shortest Subarray to be Removed to Make Array Sorted.<p>Medium</p> <p>Given an integer array <code>arr</code>, remove a subarray (can be empty) from <code>arr</code> such that the remaining elements in <code>arr</code> are <strong>non-decreasing</strong>.</p> <p>Return <em>the length of the shortest subarray to remove</em>.</p> <p>A <strong>subarray</strong> is a contiguous subsequence of the array.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> arr = [1,2,3,10,4,2,3,5]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> 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].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> arr = [5,4,3,2,1]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> 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].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> arr = [1,2,3]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The array is already non-decreasing. We do not need to remove any elements.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 10<sup>5</sup></code></li> <li><code>0 <= arr[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findLengthOfShortestSubarray

      public int findLengthOfShortestSubarray(int[] arr)