[LeetCode] 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:
  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 <=.

Thought process:
Sorting solution is trivial.
Use a stack to store the indices.
  1. Iterate through the array from start to end. Push indices to stack. Pop indices from stack while the top number on stack is larger than current number.
  2. Iterate through the array from end to start. Push indices to stack. Pop indices from stack while the top number on stack is less than current number.

Solution 1 (Stack):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int len = nums.length;
        int left = len - 1;
        int right = 0;
        Stack<Integer> stack = new Stack<>();
        
        for (int i = 0; i < len; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
                left = Math.min(left, stack.pop());
            }
            stack.push(i);
        }
        
        stack.clear();
        for (int i = len - 1; i >= 0; i--) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
                right = Math.max(right, stack.pop());
            }
            stack.push(i);
        }
        return left < right ? right - left + 1 : 0;
    }
}

Space complexity: O(n). Time complexity: O(n).

A better solution is to find the min and max of the unsorted subarray, and find their correct positions.

Solution 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int len = nums.length;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        
        for (int i = 0; i < len - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                i++;
                while (i < len) {
                    min = Math.min(min, nums[i]);
                    i++;
                }
            }
        }
        for (int i = len - 1; i > 0; i--) {
            if (nums[i] < nums[i - 1]) {
                i--;
                while (i >= 0) {
                    max = Math.max(max, nums[i]);
                    i--;
                }
            }
        }
        
        int left = nums.length;
        int right = 0;
        for (int i = 0; i < len; i++) {
            if (nums[i] > min) {
                left = i;
                break;
            }
        }
        for (int i = len - 1; i >= 0; i--) {
            if (nums[i] < max) {
                right = i;
                break;
            }
        }
        return left < right ? right - left + 1 : 0;
    }
}

This solution has constant time and space complexity.

Comments

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula