<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,2,3,10,4,2,3,5]
<strong>Output:</strong> 3
<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].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [5,4,3,2,1]
<strong>Output:</strong> 4
<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].
</pre>

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
        # 根据题意 你要删除一段区间 删除后 满足 剩下的单调递增
        #也就是说 假如你删除的是left - right 这一段
        #那么你删除完的一定满足 0 - left-1 单调递增， right - end 单调递增 且 right > left - 1
        # 那么你从后向前看 前一个 > 后一个， 最后right就是前一个大于后一个的那后一个 也就是违背的点
        n = len(arr)
        right = n - 1
        while right > 0 and arr[right-1] <= arr[right]:
            right -= 1
        if right == 0: return 0
        res = right
        left = 0

        while left == 0 or arr[left-1] <= arr[left]:
            while right < n and arr[right] < arr[left]:
                right += 1 #现在这个位置j<i, j之后的一定大于j，所以你要找大的元素
            res = min(res, right - left - 1)
        # 这个未知的right - left - 1 --->  right:end 满足单调增， 0:left 满足单调增
# left - right 之间不满足 left - right是right - left + 1 - 2(right and left) -- right-left-1
            left += 1
        return res

30100