[link](https://leetcode.com/problems/longest-turbulent-subarray/)
---
Given an integer array arr, return the length of a maximum size turbulent subarray of arr.
A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if:
- For i <= k < j:
- arr[k] > arr[k + 1] when k is odd, and
- arr[k] < arr[k + 1] when k is even.
- Or, for i <= k < j:
- arr[k] > arr[k + 1] when k is even, and
- arr[k] < arr[k + 1] when k is odd.
#### Example 1:
```
Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
```
#### Example 2:
```
Input: arr = [4,8,12,16]
Output: 2
```
#### Example 3:
```
Input: arr = [100]
Output: 1
```
#### Constraints:
- 1 <= arr.length <= 4 * 104
- 0 <= arr[i] <= 109
---
The variables L and R are initialized to 0 and 1, respectively. They represent the left and right indices of the current subarray being considered. The variables res and prev are initialized to 1 and an empty string, respectively. res stores the maximum size of a turbulent subarray found so far, and prev keeps track of the direction (">" for increasing, "<" for decreasing) of the previous comparison.
The code enters a while loop that continues as long as R is less than the length of arr. Inside the loop, the code checks if the previous element arr[R - 1] is greater than the current element arr[R], and if the previous direction is not ">" (indicating an increasing sequence). If both conditions are met, it means the subarray is becoming turbulent with a decreasing element. In this case, the res value is updated to the maximum between the current res and the size of the turbulent subarray R - L + 1. The R index is incremented, and the prev variable is updated to ">" to indicate the new direction. Similarly, if the previous element arr[R - 1] is less than the current element arr[R], and the previous direction is not "<" (indicating a decreasing sequence), it means the subarray is becoming turbulent with an increasing element. The res value is updated, the R index is incremented, and the prev variable is updated to "<".
If neither of the above conditions is met, it means the subarray is no longer turbulent. In this case, if the previous element arr[R - 1] is equal to the current element arr[R], it means the subarray is encountering equal elements. The R index is incremented to move to the next element, and the L index is set to R - 1 to start a new subarray. The prev variable is reset to an empty string. After completing the loop, the res value contains the maximum size of a turbulent subarray.
#### Solution 1
```python=
class Solution:
def maxTurbulenceSize(self, arr: List[int]) -> int:
L, R = 0, 1
res, prev = 1, ""
while R < len(arr):
if arr[R - 1] > arr[R] and prev != ">":
res = max(res, R - L + 1)
R += 1
prev = ">"
elif arr[R - 1] < arr[R] and prev != "<":
res = max(res, R - L + 1)
R += 1
prev = "<"
else:
if arr[R - 1] == arr[R]:
R = R + 1
L = R - 1
prev = ""
return res
```
O(T): O(n)
O(S): O(1)