# Leetcode 581. Shortest unsorted continuous subarray (shortest unordered contiguous subarray)

Source: Internet
Author: User

Given an integer array, the need to find one continuous subarray so if you only sort this subarray in ascending order, T Hen the whole array would 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, ten, 9, 15]output:5explanation:you need to sort [6, 4, 8, ten, 9] in ascending order to make the whole Array sorted in ascending order.`

Note:

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

Title tag: Array

The topic gives us a nums array, let's find a shortest unordered continuous subarray, and when we sort this sub-array, the entire array is already sorted.

To find the scope of this subarray, first understand how the beg and end of this range are defined.

See this example: 1 3 5 7 2 4 5 6

A. When we find the first number 2 that violates the ascending sort, we can't just mark the beg as 2 in front of a number 7, but to go straight ahead, find a suitable position, find the number above the 2 in the top position, here is 3.

B. Similarly, in order to find end, then we have to start from the back of 7, and always find the last position of the number than 7 small, here is 6.

In this case, the range is 3 to 6 is the sub-array we are looking for. After sorting 3 to 6, the entire array is already sorted.

Here we can find that 2 is min, 7 is max, so we can find beg and end in two directions.

From right to left (green), maintain update min and beg;

From left to right (red), maintain update max and end.

Java Solution:

Runtime beats 89.16%

Completion Date: 10/15/2017

Keywords: Array

Key points: Find beg and end in two directions respectively

`1 classSolution2 {3      Public intFindunsortedsubarray (int[] nums)4     {5         intn =nums.length;6         intBeg =-1;7         intend =-2;//End Is-2 is because it works if the array is already in ascending order8         intmin = nums[n-1];// from right to left9         intmax = Nums[0];// from left to rightTen          One          for(inti=0; i<n; i++) A         { -Max =Math.max (Max, nums[i]); -min = math.min (min, nums[n-1-i]); the              -             if(Nums[i] <max) -End =i; -             if(Nums[n-1-i] >min) +Beg = n-1-i; -         } +          A         returnEnd-beg + 1;//If array is already in ascending order,-2-( -1) + 1 = 0 at     } -}`

Resources:

Https://discuss.leetcode.com/topic/89282/java-o-n-time-o-1-space

Leetcode List of topics-Leetcode Questions list

Leetcode 581. Shortest unsorted continuous subarray (shortest unordered contiguous subarray)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.