<jupyter_start><jupyter_text>A subarray `A[i], A[i+1], ..., A[j]` of A is said to be turbulent if and only if:For `i  A[k+1]` when `k` is odd, and `A[k] < A[k+1]` when `k` is even;OR, for `i  A[k+1]` when `k` is even, and `A[k] < A[k+1]` when `k` is odd.That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.Return the length of a maximum size turbulent subarray of A. Example 1:    Input: [9,4,2,10,7,8,8,1,9]    Output: 5    Explanation: (A[1] > A[2]  A[4] < A[5])Example 2:    Input: [4,8,12,16]    Output: 2Example 3:    Input: [100]    Output: 1 Note:1. 1 <= A.length <= 400002. 0 <= A[i] <= 10^9<jupyter_code># sliding window with time complexity O(n)
class Solution:
    def maxTurbulenceSize(self, A):
        '''
        A: List[int]
        return: int
        '''
        res = 1
        flag = 0
        
        for i in range(1, len(A)):
            c = (A[i - 1] > A[i]) - (A[i - 1] < A[i])
            if c == 0:
                flag = i
            elif i == len(A) - 1 or c * ((A[i] > A[i + 1]) - (A[i] < A[i + 1])) != -1:
                res = max(res, i - flag + 1)
                flag = i
        return res

# test    
A = [9,4,2,10,7,8,8,1,9]
Solution().maxTurbulenceSize(A)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17246713070378963, 'fraction_numerical': 0.051817478731631866, 'mean_word_length': 2.1950617283950615, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '393016', 'license': 'MIT', 'n_tokens_mistral': 551, 'n_tokens_neox': 509, 'n_words': 162, 'path': 'DSA/slidingwindow/maxTurbulenceSize.ipynb', 'repo_name': 'lance-lh/Data-Structures-and-Algorithms', 'size': '1293'}