1574. Shortest Subarray to be Removed to Make Array Sorted (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution(object):
    def findLengthOfShortestSubarray(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        n = len(arr)
        arr = [0] + arr
        l = r = s = 0
        i, j = 0, n
        while i < n:
            if arr[i + 1] < arr[i]:
                break
            i += 1
            l += 1
        if l == n:
            return 0
        while i >= 0:
            while arr[j] >= arr[i]:
                if j < n and arr[j] > arr[j + 1]:
                    break
                j -= 1
                r += 1
            s = max(s, l + r)
            i -= 1
            l -= 1
        return n - s