Skip to content

Commit 97d0e77

Browse files
committed
fix: add solution files to lc problem: No.0581.Shortest Unsorted Continuous Subarray
1 parent dab0f40 commit 97d0e77

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

0 commit comments

Comments
 (0)