Skip to content

Commit fc01d34

Browse files
solves maximum unsorted subarray
1 parent 7aacd93 commit fc01d34

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
| 566 | [Reshape The Matrix](https://leetcode.com/problems/reshape-the-matrix) | Easy | [![Java](assets/java.png)](src/ReshapeTheMatrix.java) [![Python](assets/python.png)](python/reshape_the_matrix.py) |
153153
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree) | Easy | [![Java](assets/java.png)](src/SubtreeOfAnotherTree.java) [![Python](assets/python.png)](python/subtree_of_another_tree.py) |
154154
| 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies) | Easy | [![Java](assets/java.png)](src/DistributeCandies.java) [![Python](assets/python.png)](python/distribute_candies.py) |
155-
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray) | Easy | |
155+
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray) | Medium | [![Java](assets/java.png)](src/ShortestUnsortedContinuousSubarray.java) [![Python](assets/python.png)](python/shortest_continuous_unsorted_subarray.py) |
156156
| 589 | [N-Ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal) | Easy | |
157157
| 590 | [N-Ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal) | Easy | |
158158
| 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence) | Easy | |

Diff for: python/shortest_continuous_unsorted_subarray.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findUnsortedSubarray(self, array: List[int]) -> int:
6+
l, r, maximum, minimum = len(array), 0, array[0], array[-1]
7+
for index, value in enumerate(array):
8+
if value < maximum: r = index + 1
9+
else: maximum = value
10+
if array[len(array) - index - 1] > minimum: l = len(array) - 2 - index
11+
else: minimum = array[len(array) - index - 1]
12+
return max(0, r - l - 1)

Diff for: src/ShortestUnsortedContinuousSubarray.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ShortestUnsortedContinuousSubarray {
2+
public int findUnsortedSubarray(int[] nums) {
3+
int n = nums.length, l = n, r = 0, max = nums[0], min = nums[n - 1];
4+
for (int i = 1; i < n; i++) {
5+
if (nums[i] < max) r = i + 1;
6+
else max = nums[i];
7+
if (nums[n - 1 - i] > min) l = n - 1 - i - 1;
8+
else min = nums[n - 1 - i];
9+
}
10+
return Math.max(0, r - l - 1);
11+
}
12+
}

0 commit comments

Comments
 (0)