记录前缀max和后缀min,采用双指针拟合答案
class Solution:
def subSort(self, array: List[int]) -> List[int]:
n = len(array)
if n == 0: return [-1, -1]
pre, suf = [array[0]] * n, [array[-1]] * n
for i in range(1, n): pre[i] = max(pre[i - 1], array[i])
for i in range(n - 2, -1, -1): suf[i] = min(suf[i + 1], array[i])
l, r = 0, n - 1
while l < n:
if (l == 0 or array[l] >= array[l - 1]) and array[l] <= suf[l]: l += 1
else: break
while r >= 0:
if (r == n - 1 or array[r] <= array[r + 1]) and array[r] >= pre[r]: r -= 1
else: break
if l <= r: return [l, r]
else: return [-1, -1]