3830. Longest Alternating Subarray After Removing At Most One Element Hard

@problem@discussion
#Array#Dynamic Programming#Enumeration



1/**
2 * [3830] Longest Alternating Subarray After Removing At Most One Element
3 *
4 * You are given an integer array nums.
5 * A <span data-keyword="subarray">subarray</span> nums[l..r] is alternating if one of the following holds:
6 * 
7 * 	nums[l] < nums[l + 1] > nums[l + 2] < nums[l + 3] > ...
8 * 	nums[l] > nums[l + 1] < nums[l + 2] > nums[l + 3] < ...
9 * 
10 * In other words, if we compare adjacent elements in the subarray, then the comparisons alternate between strictly greater and strictly smaller.
11 * You can remove at most one element from nums. Then, you select an alternating subarray from nums.
12 * Return an integer denoting the maximum length of the alternating subarray you can select.
13 * A subarray of length 1 is considered alternating.
14 *  
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">nums = [2,1,3,2]</span>
18 * Output: <span class="example-io">4</span>
19 * Explanation:
20 * 
21 * 	Choose not to remove elements.
22 * 	Select the entire array [<u>2, 1, 3, 2</u>], which is alternating because 2 > 1 < 3 > 2.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">nums = [3,2,1,2,3,2,1]</span>
27 * Output: <span class="example-io">4</span>
28 * Explanation:
29 * 
30 * 	Choose to remove nums[3] i.e., [3, 2, 1, <u>2</u>, 3, 2, 1]. The array becomes [3, 2, 1, 3, 2, 1].
31 * 	Select the subarray [3, <u>2, 1, 3, 2</u>, 1].
32 * </div>
33 * <strong class="example">Example 3:
34 * <div class="example-block">
35 * Input: <span class="example-io">nums = [100000,100000]</span>
36 * Output: <span class="example-io">1</span>
37 * Explanation:
38 * 
39 * 	Choose not to remove elements.
40 * 	Select the subarray [100000, <u>100000</u>].
41 * </div>
42 *  
43 * Constraints:
44 * 
45 * 	2 <= nums.length <= 10^5
46 * 	1 <= nums[i] <= 10^5
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/longest-alternating-subarray-after-removing-at-most-one-element/
52// discuss: https://leetcode.com/problems/longest-alternating-subarray-after-removing-at-most-one-element/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn longest_alternating(nums: Vec<i32>) -> i32 {
58        0
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_3830() {
70    }
71}
72

Back
© 2026 bowen.ge All Rights Reserved.