-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path581_Shortest_Unsorted_Continuous_Subarray.py
63 lines (61 loc) · 1.88 KB
/
581_Shortest_Unsorted_Continuous_Subarray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution(object):
# https://leetcode.com/problems/shortest-unsorted-continuous-subarray/solution/
# def findUnsortedSubarray(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# snums = nums[::]
# snums.sort()
# start = len(nums)
# end = 0
# for i in range(len(nums)):
# if snums[i] != nums[i]:
# start = min(start, i)
# end = max(end, i)
# if end >= start:
# return end - start + 1
# return 0
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
stack = []
l, r = len(nums), 0
for i in range(len(nums)):
while len(stack) != 0 and nums[stack[-1]] > nums[i]:
l = min(l, stack.pop())
stack.append(i)
stack = []
for i in range(len(nums) - 1, -1, -1):
while len(stack) != 0 and nums[stack[-1]] < nums[i]:
r = max(r, stack.pop())
stack.append(i)
if r > l:
return r - l + 1
return 0
# def findUnsortedSubarray(self, nums):
# smin = 2 ** 31 -1
# smax = -2 ** 31
# flag = False
# for i in range(1, len(nums)):
# if nums[i] < nums[i-1]:
# flag = True
# if flag:
# smin = min(smin, nums[i])
# flag = False
# for i in range(len(nums)-2, -1, -1):
# if nums[i] > nums[i+1]:
# flag = True
# if flag:
# smax = max(smax, nums[i])
# for l in range(len(nums)):
# if smin < nums[l]:
# break
# for r in range(len(nums)-1, -1, -1):
# if smax > nums[r]:
# break
# if r > l:
# return r - l + 1
# return 0