Skip to content

Commit d70d09b

Browse files
add 581
1 parent 3493dae commit d70d09b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/stevesun/solutions/_581.java) | O(n) |O(1) | Easy | Array, Sort
2324
|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Solution](../master/src/main/java/com/stevesun/solutions/_575.java) | O(nlogn) |O(1) | Easy | Array
2425
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/_572.java) | O(m*n) |O(1) | Easy | Tree
2526
|567|[Permutation in String](https://leetcode.com/problems/permutation-in-string/)|[Solution](../master/src/main/java/com/stevesun/solutions/_567.java) | O(max(m,n)) |O(1) | Medium | Sliding Windows, Two Pointers
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 581. Shortest Unsorted Continuous Subarray
7+
*
8+
* 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.
9+
10+
You need to find the shortest such subarray and output its length.
11+
12+
Example 1:
13+
Input: [2, 6, 4, 8, 10, 9, 15]
14+
Output: 5
15+
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
16+
17+
Note:
18+
Then length of the input array is in range [1, 10,000].
19+
The input array may contain duplicates, so ascending order here means <=.
20+
21+
*/
22+
public class _581 {
23+
24+
/**credit: https://discuss.leetcode.com/topic/89282/java-o-n-time-o-1-space
25+
* Use start and end to keep track of the minimum subarray nums[start...end] which must be sorted for the entire array nums.
26+
* If start < end < 0 at the end of the for loop, then the array is already fully sorted.
27+
*
28+
* Time: O(n)
29+
* Space: O(1)*/
30+
public int findUnsortedSubarray(int[] nums) {
31+
int n = nums.length, start = -1, end = -2, min = nums[n-1], max = nums[0];
32+
for (int i=1;i<n;i++) {
33+
max = Math.max(max, nums[i]);
34+
min = Math.min(min, nums[n-1-i]);
35+
if (nums[i] < max) end = i;
36+
if (nums[n-1-i] > min) start = n-1-i;
37+
}
38+
return end - start + 1;
39+
}
40+
41+
/**Time: O(nlogn)
42+
* Space: O(n)*/
43+
public int findUnsortedSubarray_sorting(int[] nums) {
44+
int[] clones = nums.clone();
45+
Arrays.sort(clones);
46+
int start = nums.length;
47+
int end = 0;
48+
for (int i = 0; i < nums.length; i++) {
49+
if (clones[i] != nums[i]) {
50+
start = Math.min(start, i);
51+
end = Math.max(end, i);
52+
}
53+
}
54+
return (end - start > 0) ? end-start+1 : 0;
55+
}
56+
57+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._48;
4+
import com.stevesun.solutions._581;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 5/17/17.
12+
*/
13+
public class _581Test {
14+
private static _581 test;
15+
private static int[] nums;
16+
17+
@BeforeClass
18+
public static void setup(){
19+
test = new _581();
20+
}
21+
22+
@Test
23+
public void test1(){
24+
nums = new int[]{1,2,3,4};
25+
assertEquals(0, test.findUnsortedSubarray(nums));
26+
assertEquals(0, test.findUnsortedSubarray_sorting(nums));
27+
}
28+
29+
@Test
30+
public void test2(){
31+
nums = new int[]{2,6,4,8,10,9,15};
32+
assertEquals(5, test.findUnsortedSubarray(nums));
33+
}
34+
}

0 commit comments

Comments
 (0)