|
21 | 21 | */
|
22 | 22 | public class _581 {
|
23 | 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; |
32 |
| - int start = -1; |
33 |
| - int end = -2; |
34 |
| - int min = nums[n - 1]; |
35 |
| - int max = nums[0]; |
36 |
| - for (int i = 1; i < n; i++) { |
37 |
| - max = Math.max(max, nums[i]); |
38 |
| - min = Math.min(min, nums[n - 1 - i]); |
39 |
| - if (nums[i] < max) { |
40 |
| - end = i; |
41 |
| - } |
42 |
| - if (nums[n - 1 - i] > min) { |
43 |
| - start = n - 1 - i; |
| 24 | + public static class Solution1 { |
| 25 | + /** |
| 26 | + * credit: https://discuss.leetcode.com/topic/89282/java-o-n-time-o-1-space |
| 27 | + * Use start and end to keep track of the minimum subarray nums[start...end] which must be sorted for the entire array nums. |
| 28 | + * If start < end < 0 at the end of the for loop, then the array is already fully sorted. |
| 29 | + * |
| 30 | + * Time: O(n) |
| 31 | + * Space: O(1) |
| 32 | + */ |
| 33 | + public int findUnsortedSubarray(int[] nums) { |
| 34 | + int n = nums.length; |
| 35 | + int start = -1; |
| 36 | + int end = -2; |
| 37 | + int min = nums[n - 1]; |
| 38 | + int max = nums[0]; |
| 39 | + for (int i = 1; i < n; i++) { |
| 40 | + max = Math.max(max, nums[i]); |
| 41 | + min = Math.min(min, nums[n - 1 - i]); |
| 42 | + if (nums[i] < max) { |
| 43 | + end = i; |
| 44 | + } |
| 45 | + if (nums[n - 1 - i] > min) { |
| 46 | + start = n - 1 - i; |
| 47 | + } |
44 | 48 | }
|
| 49 | + return end - start + 1; |
45 | 50 | }
|
46 |
| - return end - start + 1; |
47 | 51 | }
|
48 | 52 |
|
49 |
| - /** |
50 |
| - * Time: O(nlogn) |
51 |
| - * Space: O(n) |
52 |
| - */ |
53 |
| - public int findUnsortedSubarray_sorting(int[] nums) { |
54 |
| - int[] clones = nums.clone(); |
55 |
| - Arrays.sort(clones); |
56 |
| - int start = nums.length; |
57 |
| - int end = 0; |
58 |
| - for (int i = 0; i < nums.length; i++) { |
59 |
| - if (clones[i] != nums[i]) { |
60 |
| - start = Math.min(start, i); |
61 |
| - end = Math.max(end, i); |
| 53 | + public static class Solution2 { |
| 54 | + /** |
| 55 | + * Time: O(nlogn) |
| 56 | + * Space: O(n) |
| 57 | + */ |
| 58 | + public int findUnsortedSubarray(int[] nums) { |
| 59 | + int[] clones = nums.clone(); |
| 60 | + Arrays.sort(clones); |
| 61 | + int start = nums.length; |
| 62 | + int end = 0; |
| 63 | + for (int i = 0; i < nums.length; i++) { |
| 64 | + if (clones[i] != nums[i]) { |
| 65 | + start = Math.min(start, i); |
| 66 | + end = Math.max(end, i); |
| 67 | + } |
62 | 68 | }
|
| 69 | + return (end - start > 0) ? end - start + 1 : 0; |
63 | 70 | }
|
64 |
| - return (end - start > 0) ? end - start + 1 : 0; |
65 | 71 | }
|
66 | 72 |
|
67 | 73 | }
|
0 commit comments