581.Shortest-Unsorted-Continuous-Subarray

581. Shortest Unsorted Continuous Subarray

题目地址

https://leetcode.com/problems/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 <=.

䝣码

Approach #1 Brute Force

O(n^2) O(1)

class Solution {
  public int findUnsortedSubarray(int[] nums) {
        int l = nums.length, r = 0;
    for (int i = 0; i < nums.length - 1; i++) {
      for (int j = i+1; j < nums.length; j++) {
        if (nums[j] < nsum[i]) {
          r = Math.max(r, j);
          l = Math.min(l, i);
        }
      }
    }
    return r - 1 < 0 ? 0 : r - l + 1;
  }
}

Approach #2 Sorting

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);
  }
}

Approach #3 Stack

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.isEmtpy() && 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.isEmtpy() && nums[stack.peek()] < nums[i]) {
        r = Math.max(r, stack.pop()); // 弹掉小
      }
      stack.push(i); // 递减栈 >
    }
    return r - l > 0 ? r - l + 1 : 0;
  }
}

Approach #4 Without Using Extra Space

Time O(n) && Space O(1)

class Solution {
  public int findUnsortedSubarray(int[] nums) {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    boolean flag = false;
    for (int i = 1; i < nums.length; i++) {
      if (nums[i] < nums[i - 1])      flag = true;
      if (flag)         min = Math.min(min, nums[i]);
    }

    flag = false;
    for (int i = nums.length - 2; i >= 0; i--) {
      if (nums[i] > nums[i + 1])     flag = true;
      if (flag)     max = Math.max(max, nums[i]);
    }

    int l, r;
    for (l = 0; l < nums.length; l++) {
      if (min < nums[l]) break;
    }
    for (r = nums.length - 1; r >= 0; r--) {
      if (max > nums[r])    break;
    }

    return r - 1 < 0 ? 0 : r - l + 1;
  }
}

Last updated