Advertisement
1988coder

581. Shortest Unsorted Continuous Subarray

Jan 5th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * Copy and Sort the input array. (Tell this first in the interview)
  5.  *
  6.  * Time Complexity: O(N log N)
  7.  *
  8.  * Space Complexity: O(N)
  9.  *
  10.  * N = Length of the input nums array.
  11.  */
  12. class Solution1 {
  13.     public int findUnsortedSubarray(int[] nums) {
  14.         if (nums == null || nums.length <= 1) {
  15.             return 0;
  16.         }
  17.  
  18.         int[] numsCopy = Arrays.copyOf(nums, nums.length);
  19.         Arrays.sort(numsCopy);
  20.  
  21.         int left = 0;
  22.         int right = nums.length - 1;
  23.  
  24.         while (left < nums.length && nums[left] == numsCopy[left]) {
  25.             left++;
  26.         }
  27.         while (left < right && nums[right] == numsCopy[right]) {
  28.             right--;
  29.         }
  30.  
  31.         return right - left + 1;
  32.     }
  33. }
  34.  
  35. /**
  36.  * Two pass Solution (Skip this in interview)
  37.  *
  38.  * Time Complexity: O(N)
  39.  *
  40.  * Space Complexity: O(1)
  41.  *
  42.  * N = Length of the input nums array.
  43.  */
  44. class Solution2 {
  45.     public int findUnsortedSubarray(int[] nums) {
  46.         if (nums == null || nums.length <= 1) {
  47.             return 0;
  48.         }
  49.  
  50.         int left = 0;
  51.         while (left < nums.length - 1 && nums[left] <= nums[left + 1]) {
  52.             left++;
  53.         }
  54.         if (left == nums.length - 1) {
  55.             return 0;
  56.         }
  57.  
  58.         int right = nums.length - 1;
  59.         while (nums[right] >= nums[right - 1]) {
  60.             right--;
  61.         }
  62.  
  63.         int min = Integer.MAX_VALUE;
  64.         int max = Integer.MIN_VALUE;
  65.         for (int i = left; i <= right; i++) {
  66.             max = Math.max(max, nums[i]);
  67.             min = Math.min(min, nums[i]);
  68.         }
  69.  
  70.         while (left >= 0 && min < nums[left]) {
  71.             left--;
  72.         }
  73.         left++;
  74.         while (right < nums.length && max > nums[right]) {
  75.             right++;
  76.         }
  77.         right--;
  78.  
  79.         return right - left + 1;
  80.     }
  81. }
  82.  
  83. /**
  84.  * One Pass Solution (Best Solution)
  85.  *
  86.  * Time Complexity: O(N)
  87.  *
  88.  * Space Complexity: O(1)
  89.  *
  90.  * N = Length of the input nums array.
  91.  */
  92. class Solution {
  93.     public int findUnsortedSubarray(int[] nums) {
  94.         if (nums == null || nums.length <= 1) {
  95.             return 0;
  96.         }
  97.  
  98.         int left = -1;
  99.         int right = -2;
  100.         int max = Integer.MIN_VALUE;
  101.         int min = Integer.MAX_VALUE;
  102.         int len = nums.length;
  103.  
  104.         for (int i = 0, j = len - 1; i < len; i++, j--) {
  105.             max = Math.max(max, nums[i]);
  106.             if (nums[i] < max) {
  107.                 right = i;
  108.             }
  109.  
  110.             min = Math.min(min, nums[j]);
  111.             if (nums[j] > min) {
  112.                 left = j;
  113.             }
  114.         }
  115.  
  116.         return right - left + 1;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement