581 Shortest Unsorted Continuous Subarray

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:

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:

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

解释下题目:

找出一个最小的子数组,然后只要这个子数组是升序的,那整个数组就是升序的

1. 错误的方法

实际耗时:

public int findUnsortedSubarray(int[] nums) {
    if (nums == null || nums.length < 2) {
        return 0;
    }
    if (nums.length == 2) {
        return nums[0] <= nums[1] ? 0 : 2;
    }

    int leftIndex = Integer.MAX_VALUE;
    int rightIndex = Integer.MAX_VALUE;
    int leftValue = Integer.MIN_VALUE;
    int rightValue = Integer.MIN_VALUE;

    for (int i = 1; i < nums.length; i++) {
        if (nums[i - 1] > nums[i]) {
            leftIndex = i - 1;
            leftValue = nums[leftIndex];
            break;
        }
    }
    Logger.getGlobal().info("first left = " + leftIndex);
    if (leftIndex == Integer.MAX_VALUE) {
        return 0;
    }
    for (int i = nums.length - 2; i >= 0; i--) {
        if (nums[i + 1] < nums[i]) {
            rightIndex = i + 1;
            rightValue = nums[rightIndex];
            break;
        }
    }
    if (rightIndex == Integer.MAX_VALUE) {
        return nums.length;
    }
    Logger.getGlobal().info("first right =" + rightIndex);

    for (int i = leftIndex - 1; i >= 0; i--) {
        if (nums[i] == leftValue) {
            leftIndex--;
        } else {
            break;
        }
    }

    Logger.getGlobal().info("second left = " + leftIndex);

    for (int i = rightIndex + 1; i < nums.length; i++) {
        if (nums[i] == rightValue) {
            rightIndex++;
        } else {
            break;
        }
    }

    Logger.getGlobal().info("second right =" + rightIndex);

    return rightIndex - leftIndex + 1;
}
踩过的坑:太多了........

  我上面的思路就是从左到右,找出第一个出现下跌的下标,然后从右到左找出第一个上升的下标,并且对等于的数字进行了“延伸”,最后算一下这两个的距离就行了。但是这种方法本质上是错的。反例:{1,2,4,5,3} 显然这个数组放到我的题目中会先找到左边的下标是3,对应了数字5;然后右边的下标是4,对应数字3,就没有然后了....


2. 网上找的方法

实际耗时:2ms

public int findUnsortedSubarray(int[] nums) {
    int n = nums.length;
    int begin = -1;
    int end = -2;
    int min = nums[n - 1];
    int max = nums[0];
    for (int i = 1; i < n; i++) {
        max = Math.max(max, nums[i]);
        min = Math.min(min, nums[n - 1 - i]);
        if (nums[i] < max) {
            end = i;
        }
        if (nums[n - 1 - i] > min) {
            begin = n - 1 - i;
        }
    }
    return end - begin + 1;
}

  begin和end的初始值只是为了最后end-begin+1得出的结论是0. 然后一开始看到这个算法我是懵的,为什么最后end反而是数组从右往左不断更新,而begin却从左到右更新呢?有这么一个结论,你得到的子数组中最大的那个数肯定小于等于原数组右边的一个数(先别考虑极限情况),同样的,你的子数组中最小的数字肯定要大于等于原数组左边的一个数。然后只要你遍历整个数组,发现有一个数字小于当前的最大值,那么这个数肯定至少会在这里结束。举个例子,{1,7,5,3}这个数组,当你遍历到5的时候,发现了5比之前遍历过的数字小,那它至少会是数组结束的一个可能解(当然发现后面3更小,所以是3)

时间复杂度O(n^2)
空间复杂度O(1)

你可能感兴趣的:(581 Shortest Unsorted Continuous Subarray)