Shortest Unsorted Continuous Subarray

Problem

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.

You need to find the shortest such subarray and output its length.

Example 1:
Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Note:

  • Then length of the input array is in range [1, 10,000].
  • The input array may contain duplicates, so ascending order here means <=.

Solution

Brute Force: O(n^2) time

public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        if (nums == null || nums.length < 2) return 0;
        int start = nums.length - 1, end = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j]) {
                    start = Math.min(start, i);
                    end = Math.max(end, j);
                }
            }
        }
        return end - start <= 0 ? 0 : end - start + 1;
    }
}

Sorted Solution: O(nlgn) time, O(n) space

public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        if (nums == null || nums.length < 2) return 0;
        int[] array = Arrays.copyOf(nums, nums.length); //Must declare length in calling of Arrays.copyOf()
        Arrays.sort(array);
        int start = nums.length - 1, end = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != array[i]) {
                start = Math.min(start, i);
                end = Math.max(end, i);
            }
        }
        return end - start <= 0 ? 0 : end - start + 1;
    }
}

O(n) time O(1) space Solution

public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        if (nums == null || nums.length < 2) return 0;
        int len = nums.length, start = len - 1, end = 0;
        int max = nums[0], min = nums[len - 1];
        for (int i = 1; i < len; i++) {
            max = Math.max(max, nums[i]); //from left to right, we find max
            min = Math.min(min, nums[len - 1 - i]); // from right to left, we find min
            if (nums[i] < max) end = i;
            if (nums[len - 1 - i] > min) start = len - 1 - i;
        }
        return end - start <= 0 ? 0 : end - start + 1;
    }
}

Analysis

I personally think this is a great interview question
All my thoughts and progress are shown above We use the same idea in all three solutions which is to find end and start of unsorted array
In the brute force solution, we use nested loop to compare nums[i] and nums[j]
And we update start and end if nums[j] < nums[i], which means is decreasing
We optimize this solution by using sort() and check statement becomes if nums[i] != sortedArray[i]

In the third solution, we still have start and end However, we divide the array into two parts
We find the max from left to right, and set end = i if that element is smaller than max
We find the min from right to left, and set start = len - 1 - i if that element is greater than min From left to right, the numbers should be increasing
Hence we need to do updates as long as we find nums[i] < max Because it' from left to right, we need to update end
From right to left, the numbers should be decreasing
Hence we need to do updates as long as we find nums[len - 1 - i] > min
Because it's from right to left, we need to update start

results matching ""

    No results matching ""