|
| 1 | +# [2765. Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray) |
| 2 | + |
| 3 | +[中文文档](/solution/2700-2799/2765.Longest%20Alternating%20Subarray/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A subarray <code>s</code> of length <code>m</code> is called <strong>alternating</strong> if:</p> |
| 8 | + |
| 9 | +<ul> |
| 10 | + <li><code>m</code> is greater than <code>1</code>.</li> |
| 11 | + <li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>.</li> |
| 12 | + <li>The 0-indexed subarray <code>s</code> looks like <code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>. In other words, <code>s<sub>1</sub> - s<sub>0</sub> = 1</code>, <code>s<sub>2</sub> - s<sub>1</sub> = -1</code>, <code>s<sub>3</sub> - s<sub>2</sub> = 1</code>, <code>s<sub>4</sub> - s<sub>3</sub> = -1</code>, and so on up to <code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>.</li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Return <em>the maximum length of all <strong>alternating</strong> subarrays present in </em><code>nums</code> <em>or </em><code>-1</code><em> if no such subarray exists</em><em>.</em></p> |
| 16 | + |
| 17 | +<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> nums = [2,3,4,3,4] |
| 24 | +<strong>Output:</strong> 4 |
| 25 | +<strong>Explanation:</strong> The alternating subarrays are [3, 4], [3, 4, 3], and [3, 4, 3, 4]. The longest of these is [3,4,3,4], which is of length 4. |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 2:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> nums = [4,5,6] |
| 32 | +<strong>Output:</strong> 2 |
| 33 | +<strong>Explanation:</strong> [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>2 <= nums.length <= 100</code></li> |
| 41 | + <li><code>1 <= nums[i] <= 10<sup>4</sup></code></li> |
| 42 | +</ul> |
| 43 | + |
| 44 | +## Solutions |
| 45 | + |
| 46 | +**Solution 1: Enumeration** |
| 47 | + |
| 48 | +We can enumerate the left endpoint $i$ of the subarray, and for each $i$, we need to find the longest subarray that satisfies the condition. We can start traversing to the right from $i$, and each time we encounter adjacent elements whose difference does not satisfy the alternating condition, we find a subarray that satisfies the condition. We can use a variable $k$ to record whether the difference of the current element should be $1$ or $-1$. If the difference of the current element should be $-k$, then we take the opposite of $k$. When we find a subarray $nums[i..j]$ that satisfies the condition, we update the answer to $\max(ans, j - i + 1)$. |
| 49 | + |
| 50 | +The time complexity is $O(n^2)$, where $n$ is the length of the array. We need to enumerate the left endpoint $i$ of the subarray, and for each $i$, we need $O(n)$ time to find the longest subarray that satisfies the condition. The space complexity is $O(1)$. |
| 51 | + |
| 52 | +<!-- tabs:start --> |
| 53 | + |
| 54 | +### **Python3** |
| 55 | + |
| 56 | +```python |
| 57 | +class Solution: |
| 58 | + def alternatingSubarray(self, nums: List[int]) -> int: |
| 59 | + ans, n = -1, len(nums) |
| 60 | + for i in range(n): |
| 61 | + k = 1 |
| 62 | + j = i |
| 63 | + while j + 1 < n and nums[j + 1] - nums[j] == k: |
| 64 | + j += 1 |
| 65 | + k *= -1 |
| 66 | + if j - i + 1 > 1: |
| 67 | + ans = max(ans, j - i + 1) |
| 68 | + return ans |
| 69 | +``` |
| 70 | + |
| 71 | +### **Java** |
| 72 | + |
| 73 | +```java |
| 74 | +class Solution { |
| 75 | + public int alternatingSubarray(int[] nums) { |
| 76 | + int ans = -1, n = nums.length; |
| 77 | + for (int i = 0; i < n; ++i) { |
| 78 | + int k = 1; |
| 79 | + int j = i; |
| 80 | + for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) { |
| 81 | + k *= -1; |
| 82 | + } |
| 83 | + if (j - i + 1 > 1) { |
| 84 | + ans = Math.max(ans, j - i + 1); |
| 85 | + } |
| 86 | + } |
| 87 | + return ans; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### **C++** |
| 93 | + |
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + int alternatingSubarray(vector<int>& nums) { |
| 98 | + int ans = -1, n = nums.size(); |
| 99 | + for (int i = 0; i < n; ++i) { |
| 100 | + int k = 1; |
| 101 | + int j = i; |
| 102 | + for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) { |
| 103 | + k *= -1; |
| 104 | + } |
| 105 | + if (j - i + 1 > 1) { |
| 106 | + ans = max(ans, j - i + 1); |
| 107 | + } |
| 108 | + } |
| 109 | + return ans; |
| 110 | + } |
| 111 | +}; |
| 112 | +``` |
| 113 | +
|
| 114 | +### **Go** |
| 115 | +
|
| 116 | +```go |
| 117 | +func alternatingSubarray(nums []int) int { |
| 118 | + ans, n := -1, len(nums) |
| 119 | + for i := range nums { |
| 120 | + k := 1 |
| 121 | + j := i |
| 122 | + for ; j+1 < n && nums[j+1]-nums[j] == k; j++ { |
| 123 | + k *= -1 |
| 124 | + } |
| 125 | + if t := j - i + 1; t > 1 && ans < t { |
| 126 | + ans = t |
| 127 | + } |
| 128 | + } |
| 129 | + return ans |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### **TypeScript** |
| 134 | + |
| 135 | +```ts |
| 136 | +function alternatingSubarray(nums: number[]): number { |
| 137 | + let ans = -1; |
| 138 | + const n = nums.length; |
| 139 | + for (let i = 0; i < n; ++i) { |
| 140 | + let k = 1; |
| 141 | + let j = i; |
| 142 | + for (; j + 1 < n && nums[j + 1] - nums[j] === k; ++j) { |
| 143 | + k *= -1; |
| 144 | + } |
| 145 | + if (j - i + 1 > 1) { |
| 146 | + ans = Math.max(ans, j - i + 1); |
| 147 | + } |
| 148 | + } |
| 149 | + return ans; |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### **...** |
| 154 | + |
| 155 | +``` |
| 156 | +
|
| 157 | +``` |
| 158 | + |
| 159 | +<!-- tabs:end --> |
0 commit comments