#3830
Longest Alternating Subarray After Removing At Most One Element
hard · 30.4% accepted · 68 likes · top 8%
array · dynamic programming · enumeration
Description
You are given an integer array nums.
A subarray nums[l..r] is alternating if one of the following holds:
- nums[l] < nums[l + 1] > nums[l + 2] < nums[l + 3] > ...
- nums[l] > nums[l + 1] < nums[l + 2] > nums[l + 3] < ...
In other words, if we compare adjacent elements in the subarray, then the comparisons alternate between strictly greater and strictly smaller.
You can remove at most one element from nums. Then, you select an alternating subarray from nums.
Return an integer denoting the maximum length of the alternating subarray you can select.
A subarray of length 1 is considered alternating.
Solution