978. Longest Turbulent Subarray Medium
1/**
2 * [978] Longest Turbulent Subarray
3 *
4 * Given an integer array arr, return the length of a maximum size turbulent subarray of arr.
5 * A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
6 * More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if:
7 *
8 * For i <= k < j:
9 *
10 * arr[k] > arr[k + 1] when k is odd, and
11 * arr[k] < arr[k + 1] when k is even.
12 *
13 *
14 * Or, for i <= k < j:
15 *
16 * arr[k] > arr[k + 1] when k is even, and
17 * arr[k] < arr[k + 1] when k is odd.
18 *
19 *
20 *
21 *
22 * Example 1:
23 *
24 * Input: arr = [9,4,2,10,7,8,8,1,9]
25 * Output: 5
26 * Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
27 *
28 * Example 2:
29 *
30 * Input: arr = [4,8,12,16]
31 * Output: 2
32 *
33 * Example 3:
34 *
35 * Input: arr = [100]
36 * Output: 1
37 *
38 *
39 * Constraints:
40 *
41 * 1 <= arr.length <= 4 * 10^4
42 * 0 <= arr[i] <= 10^9
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/longest-turbulent-subarray/
48// discuss: https://leetcode.com/problems/longest-turbulent-subarray/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn max_turbulence_size(arr: Vec<i32>) -> i32 {
54 0
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_978() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.