-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path581.Shortest-Unsorted-Continuous-Subarray.py
58 lines (48 loc) · 1.41 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
# https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/
#
# algorithms
# Easy (29.26%)
# Total Accepted: 45k
# Total Submissions: 153.8k
# beats 15.59% of python submissions
class Solution(object):
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
sorted_nums = map(lambda (idx, n): (n, idx), enumerate(nums))
sorted_nums.sort()
head, tail = 0, length - 1
for i in xrange(length):
head = i
if sorted_nums[i][1] != i:
break
for i in xrange(length - 1, head + 1, -1):
tail = i
if sorted_nums[i][1] != i:
break
return tail - head + 1
class Solution1(object):
def findUnsortedSubarray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
if length < 2:
return 0
cur_min, l = nums[-1], -1
for i in xrange(length - 2, -1, -1):
if nums[i] < cur_min:
cur_min = nums[i]
elif nums[i] > cur_min:
l = i
cur_max, r = nums[0], -1
for i in xrange(length):
if nums[i] > cur_max:
cur_max = nums[i]
elif nums[i] < cur_max:
r = i
return 0 if l == r else r - l + 1