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 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 <=.

数组的题常用方法 :排序、最大值指针边走边找、最小值指针边走边找,数组的其他指针位置—顺序遍历,

逆序遍历,其他指针与最大值指针比较的后果或最小值指针

先想排序能否做:

public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int[] snums = nums.clone();
        Arrays.sort(snums);
        int start = snums.length, end = 0;
        for (int i = 0; i < snums.length; i++) {
            if (snums[i] != nums[i]) {
                start = Math.min(start, i);
                end = Math.max(end, i);
            }
        }
        return (end - start >= 0 ? end - start + 1 : 0);
    }
}

 

Stack 画图: 利用相对位置和大小pop找到最左边的位置, 和最右边的位置, 利用栈内的元素都是最小的特点, 找到要排序的sub 中最小的点应该在的排序好后的位置; 利用栈内的元素都是最大的特点, 找到要排序的sub 中最大的点应该在的排序好后的位置; 此位置即是sub数组的要排序的起点和终点

会画图找位置

 

In order to determine the correct position of nums[j]nums[j],

we keep on popping the elemnents from the top of the stackstack until we reach the stage where the element(corresponding to the index)

on the top of the stackstack is lesser than nums[j]nums[j]. 

public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        Stack < Integer > stack = new Stack < Integer > ();
        int l = nums.length, r = 0;
        for (int i = 0; i < nums.length; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] > nums[i])
                l = Math.min(l, stack.pop());
            stack.push(i);
        }
        stack.clear();
        for (int i = nums.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i])
                r = Math.max(r, stack.pop());
            stack.push(i);
        }
        return r - l > 0 ? r - l + 1 : 0;
    }
}

 

Complexity Analysis

  • Time complexity : O(n)O(n). Stack of size nn is filled.

  • Space complexity : O(n)O(n). Stack size grows upto nn.

因为只需要记坐标和最大值最小值,  可将上述的栈空间优化, 但是对遍历的顺序有要求-- 还是 找sub 里最大点应该在的最右的位置顺序-- 栈是因为可以回退到右边的, 最小点应该在的最左边的位置---栈可以回退的; 因为最值都可找到, 但是最左最右是对遍历顺序有要求的

public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        if (n == 0 || nums == null) {
            return 0;
        }
        int max = nums[0], min = nums[n - 1], beg = - 2, end = - 1;
        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) beg = n - 1- i;
        }
        return end - beg + 1;
}

  

 

posted @ 2017-08-06 22:34  apanda009  阅读(174)  评论(0编辑  收藏  举报