-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path581.cpp
More file actions
21 lines (21 loc) · 665 Bytes
/
Copy path581.cpp
File metadata and controls
21 lines (21 loc) · 665 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int findUnsortedSubarray(vector<int> &nums) {
int max_val = INT_MIN, min_val = INT_MAX, start = -1, end = -1, n = nums.size();
for (int i = 0; i < n - 1; ++i) {
if (nums[i] > nums[i + 1]) {
max_val = max(max_val, nums[i]);
min_val = min(min_val, nums[i + 1]);
}
}
for (start = 0; start < n; ++start) {
if (nums[start] > min_val)
break;
}
for (end = n - 1; end >= 0; --end) {
if (nums[end] < max_val)
break;
}
return start > end ? 0 : end - start + 1;
}
};