Skip to content

Commit c184c59

Browse files
committed
added Shorted Unsorted Continuous Subarray (easy)
1 parent 7d4c6c5 commit c184c59

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Shortest Unsorted Continuous Subarray
3+
[Leetcode Link](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)
4+
5+
## Problem:
6+
7+
Given an integer array, you need to find one **continuous subarray** that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
8+
9+
You need to find the **shortest** such subarray and output its length.
10+
11+
## Example:
12+
13+
```
14+
Input: [2, 6, 4, 8, 10, 9, 15]
15+
Output: 5
16+
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
17+
```
18+
19+
## Note:
20+
21+
1. Then length of the input array is in range `[1, 10,000]`.
22+
2. The input array may contain duplicates, so ascending order here means `<=`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Arrays;
2+
3+
// Sort array and check the length of unmatching subarray
4+
5+
class Solution {
6+
public int findUnsortedSubarray(int[] nums) {
7+
int[] sorted = Arrays.copyOf(nums, nums.length);
8+
Arrays.sort(sorted);
9+
// System.out.println(Arrays.toString(sorted));
10+
// System.out.println(Arrays.toString(nums));
11+
int start = 0;
12+
int end = 0;
13+
// first difference is the start index of contiguous subarray
14+
for (int i = 0; i < nums.length; i++) {
15+
if (nums[i] != sorted[i]) {
16+
start = i;
17+
break;
18+
}
19+
}
20+
// first difference from backwards is the end index of contiguous subarray
21+
for (int i = nums.length - 1; i >= 0; i--) {
22+
if (nums[i] != sorted[i]) {
23+
end = i;
24+
break;
25+
}
26+
}
27+
// System.out.println(start);
28+
// System.out.println(end);
29+
30+
return start != end ? end - start + 1 : 0;
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.util.Arrays;
2+
3+
class TestDriver {
4+
public static void main(String[] args) {
5+
Solution sol = new Solution();
6+
int[] input = { 2, 6, 4, 8, 10, 9, 15 };
7+
System.out.println("Input: " + Arrays.toString(input));
8+
System.out.println("Output: " + sol.findUnsortedSubarray(input));
9+
System.out.println();
10+
}
11+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Languages used: Java and Python
2727
- [House Robber](Easy/HouseRobber)
2828
- [Majority Element](Easy/MajorityElement)
2929
- [Reshape the Matrix](Easy/ReshapeMatrix)
30+
- [Shortest Unsorted Continuous Subarray](Easy/ShortedUnsortedContinuousSubarray)
3031
- Medium
3132
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
3233
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)