java.lang.Object
g0901_1000.s0978_longest_turbulent_subarray.Solution

public class Solution extends Object
978 - Longest Turbulent Subarray.<p>Medium</p> <p>Given an integer array <code>arr</code>, return <em>the length of a maximum size turbulent subarray of</em> <code>arr</code>.</p> <p>A subarray is <strong>turbulent</strong> if the comparison sign flips between each adjacent pair of elements in the subarray.</p> <p>More formally, a subarray <code>[arr[i], arr[i + 1], ..., arr[j]]</code> of <code>arr</code> is said to be turbulent if and only if:</p> <ul> <li>For <code>i <= k < j</code>: <ul> <li><code>arr[k] > arr[k + 1]</code> when <code>k</code> is odd, and</li> <li><code>arr[k] < arr[k + 1]</code> when <code>k</code> is even.</li> </ul> </li> <li>Or, for <code>i <= k < j</code>: <ul> <li><code>arr[k] > arr[k + 1]</code> when <code>k</code> is even, and</li> <li><code>arr[k] < arr[k + 1]</code> when <code>k</code> is odd.</li> </ul> </li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> arr = [9,4,2,10,7,8,8,1,9]</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> arr[1] > arr[2] < arr[3] > arr[4] < arr[5]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> arr = [4,8,12,16]</p> <p><strong>Output:</strong> 2</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> arr = [100]</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 4 * 10<sup>4</sup></code></li> <li><code>0 <= arr[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxTurbulenceSize

      public int maxTurbulenceSize(int[] arr)