LeetCode Solutions

978. Longest Turbulent Subarray

Time: $O(n)$

Space: $O(1)$

			

class Solution {
 public:
  int maxTurbulenceSize(vector<int>& A) {
    int ans = 1;
    int increasing = 1;
    int decreasing = 1;

    for (int i = 1; i < A.size(); ++i) {
      if (A[i] > A[i - 1]) {
        increasing = decreasing + 1;
        decreasing = 1;
      } else if (A[i] < A[i - 1]) {
        decreasing = increasing + 1;
        increasing = 1;
      } else {
        increasing = 1;
        decreasing = 1;
      }
      ans = max({ans, increasing, decreasing});
    }

    return ans;
  }
};
			

class Solution {
  public int maxTurbulenceSize(int[] A) {
    int ans = 1;
    int increasing = 1;
    int decreasing = 1;

    for (int i = 1; i < A.length; ++i) {
      if (A[i] > A[i - 1]) {
        increasing = decreasing + 1;
        decreasing = 1;
      } else if (A[i] < A[i - 1]) {
        decreasing = increasing + 1;
        increasing = 1;
      } else {
        increasing = 1;
        decreasing = 1;
      }
      ans = Math.max(ans, Math.max(increasing, decreasing));
    }

    return ans;
  }
}
			

class Solution:
  def maxTurbulenceSize(self, A: List[int]) -> int:
    ans = 1
    increasing = 1
    decreasing = 1

    for i in range(1, len(A)):
      if A[i] > A[i - 1]:
        increasing = decreasing + 1
        decreasing = 1
      elif A[i] < A[i - 1]:
        decreasing = increasing + 1
        increasing = 1
      else:
        increasing = 1
        decreasing = 1
      ans = max(ans, max(increasing, decreasing))

    return ans