581. 最短无序连续子数组
为保证权益,题目请参考 581. 最短无序连续子数组(From LeetCode).
解决方案1
Python
python
# 581. 最短无序连续子数组
# https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/
from typing import List
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
maxn, right = float("-inf"), -1
minn, left = float("inf"), -1
for i in range(n):
if maxn > nums[i]:
right = i
else:
maxn = nums[i]
if minn < nums[n - i - 1]:
left = n - i - 1
else:
minn = nums[n - i - 1]
return 0 if right == -1 else right - left + 1
def test_1():
solution = Solution()
assert solution.findUnsortedSubarray([2, 3, 3, 2, 4]) == 3
if __name__ == "__main__":
solution = Solution()
test_1()
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
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