Skip to content

Latest commit

 

History

History
15 lines (8 loc) · 1.14 KB

581.md

File metadata and controls

15 lines (8 loc) · 1.14 KB

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

It kind like this part shall be all greater than before and less than the following value. If we find a value is smaller than before, then it must be in the shortest continous array.

It following value is smaller than first one, then it also need to add to this array. Otherwise, we only need to add current value. So the bigger to smaller pattern we need to record and put all of them into the answer array. Let me try.

Such a long time not to impelement the whole method. My coding skill is becoming poor. My idea is correct. But we can easily impelement it. In my code, we only need to record past maxx, if current value is smaller than past maxximum, it means it's a end.

BUT!!! for the start, similar idea, we can know that the start is the smallest place/index that is bigger than the value after him (following him == before him in the reverse iteration order).

AC!