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:
Then length of the input array is in range [1, 10,000].
The input array may contain duplicates, so ascending order here means <=.
C语言
int findUnsortedSubarray(int* nums, int numsSize) {
int start=0,end=0;
int max=-999,min=999;
if(numsSize<2){return 0;}
if(numsSize==2){if(nums[0]<nums[1])return 0;else return 2;}
for(int s=0;s<numsSize-1;s++){
if(nums[s+1]>=nums[s]){start++;if(start==numsSize-1){return 0;}}
else
for(int i=numsSize-1;i>start;i--){
if(nums[i-1]<=nums[i]){end++;if(end==numsSize-1){return 0;}}
else
while(1){
for(int j=start;j<numsSize-end;j++){
if(nums[j]>max)
max=nums[j];
if(nums[j]<min)
min=nums[j];
}
if(((end==0)||(max<nums[numsSize-end]))&&((start==0)||(min>nums[start-1]))){return numsSize-start-end;}
if((end!=0)&&(max>nums[numsSize-end])&&(end<numsSize))
end--;
if((end!=0)&&(max=nums[numsSize-end])&&(end<numsSize))
end++;
if((start!=0)&&(min<nums[start])&&(start<numsSize))
start--;
if((start!=0)&&(min=nums[start])&&(start<numsSize))
start++;
}
}
}
return numsSize-start-end;
}
Time Limit Exceeded
Details
Last executed input
[1,3,2,2,2]
太容易超时了,因此还是重新建立一个数组比较合适
void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;}
void quicksort(int array[], int maxlen, int begin, int end){
int i, j;
if(begin<end){
i=begin + 1;
j=end;
while(i<j){
if(array[i]>array[begin]){
swap(&array[i], &array[j]);
j--;}
else{i++;}}
if(array[i] >= array[begin]){
i--; }
swap(&array[begin], &array[i]);
quicksort(array, maxlen, begin, i);
quicksort(array, maxlen, j, end);}}
int findUnsortedSubarray(int* nums, int numsSize) {
int a[10000];
for(int i=0;i<numsSize;++i){a[i]=nums[i];}
int start=0,end=0;
quicksort(a,numsSize,0,numsSize-1);
for(int i=0;i<numsSize;i++){
if(nums[i]==a[i]){start++;if(start==numsSize)return 0;}
else
break;}
for(int i=numsSize-1;i>0;i--){
if(nums[i]==a[i]){end++;if(end==numsSize)return 0;}
else
break;}
return numsSize-start-end;
}
Success
Details
Runtime: 156 ms, faster than 26.21% of C online submissions for Shortest Unsorted Continuous Subarray.
Memory Usage: 9 MB, less than 100.00% of C online submissions for Shortest Unsorted Continuous Subarray.
噗(/≧▽≦)/纳闷的是建立了一个新数组内存占用比100%的人少?
python3
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
if len(nums) < 2: return 0
l, r = 0, len(nums) - 1
while l < len(nums) - 1 and nums[l] <= nums[l + 1]:
l += 1
while r > 0 and nums[r] >= nums[r -1]:
r -= 1
if l > r:
return 0
temp = nums[l:r+1]
tempMin = min(temp)
tempMax = max(temp)
while l > 0 and tempMin < nums[l-1]:
l -= 1
while r < len(nums) - 1 and tempMax > nums[r+1]:
r += 1
return r - l + 1
Success
Details
Runtime: 60 ms, faster than 97.90% of Python3 online submissions for Shortest Unsorted Continuous Subarray.
Memory Usage: 14.1 MB, less than 6.80% of Python3 online submissions for Shortest Unsorted Continuous Subarray.