This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int findUnsortedSubarray(vector<int>& nums) { | |
int len = nums.size(), first = -1, last = -1, currMax = nums[0], currMin = nums[len - 1]; | |
//find first and last reverse pairs | |
for(int i = 1; i < len; ++i) | |
{ | |
if(nums[i] < currMax) | |
last = i; | |
currMax = max(currMax, nums[i]); | |
} | |
for(int i = len - 2; i >= 0; --i) | |
{ | |
if(nums[i] > currMin) | |
first = i; | |
currMin = min(currMin, nums[i]); | |
} | |
return first == -1 && last == -1? 0: last - first + 1; | |
} | |
}; |
No comments:
Post a Comment