581 Shortest Unsorted Continuous Subarray
Input:
[2, 6, 4, 8, 10, 9, 15]
Output:
5
Explanation:
You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.# this one is a gem: beat 100% submissions at the time
class Solution:
def findUnsortedSubarray(self, ar):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(ar) - 1
# find the first conflict from the left side
while (left < len(ar) - 1):
if ar[left] > ar[left + 1]:
break
left += 1
# entire array is sorted
if left == len(ar) - 1 or not ar:
return 0
# find the first conflict from the right
while (right >= 1):
if ar[right - 1] > ar[right]:
break
right -= 1
# entire array is unsorted
if left == len(ar) - 1:
return len(ar)
# now adjust left and right so that the array becomes sorted
min_l, max_r = min(ar[left:right + 1]), max(ar[left:right + 1]),
# while the element to the left is greater than min_l
while (left >= 1 and min_l < ar[left - 1]):
left -= 1
# while the element to the right is smaller than max_r
while (right < len(ar) - 1 and max_r > ar[right + 1]):
right += 1
return right - left + 1Last updated