Skip to content

Commit 64f4799

Browse files
committed
[Add] Leetcode > 581. Shortest Unsorted Continuous Subarray
1 parent 59429ea commit 64f4799

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Leetcode/shortest_subarray.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
while left <= right:
16+
if sorted_nums[right] == nums[right] : right -= 1
17+
else: break
18+
19+
return right - left + 1

0 commit comments

Comments
 (0)