File tree 2 files changed +31
-0
lines changed
solution/0500-0599/0581.Shortest Unsorted Continuous Subarray
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ func findUnsortedSubarray (nums []int ) int {
2
+ n := len (nums )
3
+ maxn , minn := math .MinInt32 , math .MaxInt32
4
+ left , right := - 1 , - 1
5
+ for i := 0 ; i < n ; i ++ {
6
+ if maxn > nums [i ] {
7
+ right = i
8
+ } else {
9
+ maxn = nums [i ]
10
+ }
11
+ if minn < nums [n - i - 1 ] {
12
+ left = n - i - 1
13
+ } else {
14
+ minn = nums [n - i - 1 ]
15
+ }
16
+ }
17
+ if right == - 1 {
18
+ return 0
19
+ }
20
+ return right - left + 1
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findUnsortedSubarray (self , nums : List [int ]) -> int :
3
+ n = len (nums )
4
+ numsSorted = sorted (nums )
5
+ left , right = 0 , n - 1
6
+ while left < n and nums [left ] == numsSorted [left ]:
7
+ left += 1
8
+ while right >= 0 and nums [right ] == numsSorted [right ]:
9
+ right -= 1
10
+ return 0 if right == - 1 else right - left + 1
You can’t perform that action at this time.
0 commit comments