0
点赞
收藏
分享

微信扫一扫

581. Shortest Unsorted Continuous Subarray

雷亚荣 2022-08-03 阅读 36


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

class Solution {
public int findUnsortedSubarray(int[] nums) {
int n = nums.length;
int beg = -1;
int end = -2; // end is -2 is because it works if the array is already in ascending order
int min = nums[n-1]; // from right to left
int max = nums[0]; // from left to right

for(int i=0; i<n; i++)
{
max = Math.max(max, nums[i]);
min = Math.min(min, nums[n-1-i]);

if(nums[i] < max)
end = i;
if(nums[n-1-i] > min)
beg = n-1-i;
}

return end - beg + 1; // if array is already in ascending order, -2 - (-1) + 1 = 0


举报

相关推荐

0 条评论