1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int maxTurbulenceSize(vector<int>& arr) { int n = arr.size(); int preSign = 0, left = 0, right = 1, ans = 1; while (right < n) { int sign = arr[right] - arr[right - 1]; if (sign == 0) { ans = max(ans, right - left); left = right; } else if ((sign > 0 && preSign > 0) || (sign < 0 && preSign < 0)) { ans = max(ans, right - left); left = right - 1; } preSign = sign; ++right; } return max(ans, right - left); } };
|