16.Shortest Unsorted Continuous Subarray
Given an integer array nums
, 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.
Return the shortest such subarray and output its length.
Solution I : (Using sorting)
Time Complexity : O(N log N), Space Complexity: O(N)
Solution II: (Using Stack)
Approach: Finding the correct position of the minimum element in the unsorted subarray helps to determine the required left boundary. Similarly, the correct position of the maximum element in the unsorted subarray helps to determine the required right boundary.
Time Complexity : O(N), Space Complexity: O(N)
Last updated