We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 59429ea commit 64f4799Copy full SHA for 64f4799
Leetcode/shortest_subarray.py
@@ -0,0 +1,19 @@
1
+# 581. Shortest Unsorted Continuous Subarray
2
+
3
+class Solution:
4
+ def findUnsortedSubarray(self, nums: List[int]) -> int:
5
+ sorted_nums = sorted(nums)
6
7
+ left, right = 0, len(nums)-1
8
9
+ if left >= right: return 0 # length <= 1
10
11
+ while left <= right:
12
+ if sorted_nums[left] == nums[left] : left += 1
13
+ else: break
14
15
16
+ if sorted_nums[right] == nums[right] : right -= 1
17
18
19
+ return right - left + 1
0 commit comments