💥 Runtime beats 100.00%, memory beats 94.70% [EXPLAINED]
Intuition\nFind the shortest part of the array to remove so the remaining elements are sorted, using the fact that the array already has a sorted prefix and suf
# Intuition\nFind the shortest part of the array to remove so the remaining elements are sorted, using the fact that the array already has a sorted prefix and suffix.\n\n# Approach\nIdentify the longest sorted prefix and suffix, then use two pointers to check if they can merge, minimizing the part to remove.\n\n# Complexity\n- Time complexity:\nO(n), because we scan the array a few times with pointers.\n\n- Space complexity:\nO(1), since no extra memory is used.\n\n# Code\n```python3 []\nclass Solution:\n    def findLengthOfShortestSubarray(self, arr: list[int]) -> int:\n        n = len(arr)\n        \n        # Find the longest non-decreasing prefix\n        left = 0\n        while left < n - 1 and arr[left] <= arr[left + 1]:\n            left += 1\n        \n        # If the entire array is already sorted\n        if left == n - 1:\n            return 0\n        \n        # Find the longest non-decreasing suffix\n        right = n - 1\n        while right > 0 and arr[right - 1] <= arr[right]:\n            right -= 1\n        \n        # Minimum length to remove\n        result = min(n - left - 1, right)  # Remove prefix or suffix\n        \n        # Check if we can merge prefix and suffix\n        i, j = 0, right\n        while i <= left and j < n:\n            if arr[i] <= arr[j]:\n                result = min(result, j - i - 1)\n                i += 1\n            else:\n                j += 1\n        \n        return result\n\n```
['Array', 'Two Pointers', 'Binary Search', 'Stack', 'Monotonic Stack', 'Python3']