<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:
        # we want to find the longest increasing subarray after removing one subarray
        # you can only keep the left or the right subarray because we can only remove one subarray, 
        # therefore nothing in the middle is okay, e.g. [dec, inc, dec] -> you would have to delete 2 subarrays
        # [1,2,3,10,4,2,3,5,11,15]
        # 1. find leftTop and rightBot portions
        leftTop = 0
        for i in range(1, len(arr)):
            if arr[i]>=arr[i-1]:
                leftTop=i
            else:
                break
        rightBot = len(arr)-1
        for i in range(len(arr)-1,0,-1):
            if arr[i]>=arr[i-1]:
                rightBot = i-1
            else:
                break
        # corner case: whole array is sorted
        if rightBot<=leftTop:
            return 0
        # take the left asc, or the right asc only
        shortest = min(len(arr)-1-leftTop, rightBot)
        
        # sliding window. Start with left=0, right = rightBot. 
        # if this can work, then we can try left+1. 
        # If this can't work, then that means there is nothing on the left that can make right work, right+1
        l = 0
        
        for r in range(rightBot, len(arr)):
            while arr[r]>=arr[l] and l<=leftTop:
                print(l, r)
                shortest = min(shortest, r-l-1)
                l+=1
        return shortest
        

30200