Skip to content

Commit 6e704f2

Browse files
authored
feat: add solutions to lc problem: No.2760 (doocs#1966)
No.2760.Longest Even Odd Subarray With Threshold
1 parent c29bab0 commit 6e704f2

File tree

7 files changed

+244
-18
lines changed

7 files changed

+244
-18
lines changed

solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/README.md

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@
6565

6666
我们在 $[0,..n-1]$ 范围内枚举所有 $l$,如果 $nums[l]$ 满足 $nums[l] \bmod 2 = 0$ 并且 $nums[l] \leq threshold$,那么我们就从 $l+1$ 开始,查找最大的满足条件的 $r$,那么此时以 $nums[l]$ 作为左端点的最长奇偶子数组的长度为 $r - l$,取所有 $r - l$ 的最大值作为答案即可。
6767

68-
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
68+
时间复杂度 $O(n^2)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
69+
70+
**方法二:枚举优化**
71+
72+
我们注意到,题目实际上会把数组划分成不相交的若干个满足条件的子数组,我们只需要找到这些子数组中最长的一个即可。因此,在枚举 $l$ 和 $r$ 时,我们不需要回退,只需要从左往右遍历一遍即可。
73+
74+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
6975

7076
<!-- tabs:start -->
7177

@@ -86,6 +92,22 @@ class Solution:
8692
return ans
8793
```
8894

95+
```python
96+
class Solution:
97+
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
98+
ans, l, n = 0, 0, len(nums)
99+
while l < n:
100+
if nums[l] % 2 == 0 and nums[l] <= threshold:
101+
r = l + 1
102+
while r < n and nums[r] % 2 != nums[r - 1] % 2 and nums[r] <= threshold:
103+
r += 1
104+
ans = max(ans, r - l)
105+
l = r
106+
else:
107+
l += 1
108+
return ans
109+
```
110+
89111
### **Java**
90112

91113
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -108,6 +130,27 @@ class Solution {
108130
}
109131
```
110132

133+
```java
134+
class Solution {
135+
public int longestAlternatingSubarray(int[] nums, int threshold) {
136+
int ans = 0;
137+
for (int l = 0, n = nums.length; l < n;) {
138+
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
139+
int r = l + 1;
140+
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
141+
++r;
142+
}
143+
ans = Math.max(ans, r - l);
144+
l = r;
145+
} else {
146+
++l;
147+
}
148+
}
149+
return ans;
150+
}
151+
}
152+
```
153+
111154
### **C++**
112155

113156
```cpp
@@ -129,11 +172,33 @@ public:
129172
};
130173
```
131174
175+
```cpp
176+
class Solution {
177+
public:
178+
int longestAlternatingSubarray(vector<int>& nums, int threshold) {
179+
int ans = 0;
180+
for (int l = 0, n = nums.size(); l < n;) {
181+
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
182+
int r = l + 1;
183+
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
184+
++r;
185+
}
186+
ans = max(ans, r - l);
187+
l = r;
188+
} else {
189+
++l;
190+
}
191+
}
192+
return ans;
193+
}
194+
};
195+
```
196+
132197
### **Go**
133198

134199
```go
135-
func longestAlternatingSubarray(nums []int, threshold int) int {
136-
ans, n := 0, len(nums)
200+
func longestAlternatingSubarray(nums []int, threshold int) (ans int) {
201+
n := len(nums)
137202
for l := range nums {
138203
if nums[l]%2 == 0 && nums[l] <= threshold {
139204
r := l + 1
@@ -143,7 +208,25 @@ func longestAlternatingSubarray(nums []int, threshold int) int {
143208
ans = max(ans, r-l)
144209
}
145210
}
146-
return ans
211+
return
212+
}
213+
```
214+
215+
```go
216+
func longestAlternatingSubarray(nums []int, threshold int) (ans int) {
217+
for l, n := 0, len(nums); l < n; {
218+
if nums[l]%2 == 0 && nums[l] <= threshold {
219+
r := l + 1
220+
for r < n && nums[r]%2 != nums[r-1]%2 && nums[r] <= threshold {
221+
r++
222+
}
223+
ans = max(ans, r-l)
224+
l = r
225+
} else {
226+
l++
227+
}
228+
}
229+
return
147230
}
148231
```
149232

@@ -166,6 +249,26 @@ function longestAlternatingSubarray(nums: number[], threshold: number): number {
166249
}
167250
```
168251

252+
```ts
253+
function longestAlternatingSubarray(nums: number[], threshold: number): number {
254+
const n = nums.length;
255+
let ans = 0;
256+
for (let l = 0; l < n; ) {
257+
if (nums[l] % 2 === 0 && nums[l] <= threshold) {
258+
let r = l + 1;
259+
while (r < n && nums[r] % 2 !== nums[r - 1] % 2 && nums[r] <= threshold) {
260+
++r;
261+
}
262+
ans = Math.max(ans, r - l);
263+
l = r;
264+
} else {
265+
++l;
266+
}
267+
}
268+
return ans;
269+
}
270+
```
271+
169272
### **...**
170273

171274
```

solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/README_EN.md

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ Hence, the answer is the length of the subarray, 3. We can show that 3 is the ma
5757

5858
## Solutions
5959

60+
**Solution 1: Enumeration**
61+
62+
We enumerate all $l$ in the range $[0,..n-1]$. If $nums[l]$ satisfies $nums[l] \bmod 2 = 0$ and $nums[l] \leq threshold$, then we start from $l+1$ to find the largest $r$ that meets the condition. At this time, the length of the longest odd-even subarray with $nums[l]$ as the left endpoint is $r - l$. We take the maximum of all $r - l$ as the answer.
63+
64+
The time complexity is $O(n^2)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
65+
66+
**Solution 2: Optimized Enumeration**
67+
68+
We notice that the problem actually divides the array into several disjoint subarrays that meet the condition. We only need to find the longest one among these subarrays. Therefore, when enumerating $l$ and $r$, we don't need to backtrack, we just need to traverse from left to right once.
69+
70+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
71+
6072
<!-- tabs:start -->
6173

6274
### **Python3**
@@ -74,6 +86,22 @@ class Solution:
7486
return ans
7587
```
7688

89+
```python
90+
class Solution:
91+
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
92+
ans, l, n = 0, 0, len(nums)
93+
while l < n:
94+
if nums[l] % 2 == 0 and nums[l] <= threshold:
95+
r = l + 1
96+
while r < n and nums[r] % 2 != nums[r - 1] % 2 and nums[r] <= threshold:
97+
r += 1
98+
ans = max(ans, r - l)
99+
l = r
100+
else:
101+
l += 1
102+
return ans
103+
```
104+
77105
### **Java**
78106

79107
```java
@@ -94,6 +122,27 @@ class Solution {
94122
}
95123
```
96124

125+
```java
126+
class Solution {
127+
public int longestAlternatingSubarray(int[] nums, int threshold) {
128+
int ans = 0;
129+
for (int l = 0, n = nums.length; l < n;) {
130+
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
131+
int r = l + 1;
132+
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
133+
++r;
134+
}
135+
ans = Math.max(ans, r - l);
136+
l = r;
137+
} else {
138+
++l;
139+
}
140+
}
141+
return ans;
142+
}
143+
}
144+
```
145+
97146
### **C++**
98147

99148
```cpp
@@ -115,11 +164,33 @@ public:
115164
};
116165
```
117166
167+
```cpp
168+
class Solution {
169+
public:
170+
int longestAlternatingSubarray(vector<int>& nums, int threshold) {
171+
int ans = 0;
172+
for (int l = 0, n = nums.size(); l < n;) {
173+
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
174+
int r = l + 1;
175+
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
176+
++r;
177+
}
178+
ans = max(ans, r - l);
179+
l = r;
180+
} else {
181+
++l;
182+
}
183+
}
184+
return ans;
185+
}
186+
};
187+
```
188+
118189
### **Go**
119190

120191
```go
121-
func longestAlternatingSubarray(nums []int, threshold int) int {
122-
ans, n := 0, len(nums)
192+
func longestAlternatingSubarray(nums []int, threshold int) (ans int) {
193+
n := len(nums)
123194
for l := range nums {
124195
if nums[l]%2 == 0 && nums[l] <= threshold {
125196
r := l + 1
@@ -129,7 +200,25 @@ func longestAlternatingSubarray(nums []int, threshold int) int {
129200
ans = max(ans, r-l)
130201
}
131202
}
132-
return ans
203+
return
204+
}
205+
```
206+
207+
```go
208+
func longestAlternatingSubarray(nums []int, threshold int) (ans int) {
209+
for l, n := 0, len(nums); l < n; {
210+
if nums[l]%2 == 0 && nums[l] <= threshold {
211+
r := l + 1
212+
for r < n && nums[r]%2 != nums[r-1]%2 && nums[r] <= threshold {
213+
r++
214+
}
215+
ans = max(ans, r-l)
216+
l = r
217+
} else {
218+
l++
219+
}
220+
}
221+
return
133222
}
134223
```
135224

@@ -152,6 +241,26 @@ function longestAlternatingSubarray(nums: number[], threshold: number): number {
152241
}
153242
```
154243

244+
```ts
245+
function longestAlternatingSubarray(nums: number[], threshold: number): number {
246+
const n = nums.length;
247+
let ans = 0;
248+
for (let l = 0; l < n; ) {
249+
if (nums[l] % 2 === 0 && nums[l] <= threshold) {
250+
let r = l + 1;
251+
while (r < n && nums[r] % 2 !== nums[r - 1] % 2 && nums[r] <= threshold) {
252+
++r;
253+
}
254+
ans = Math.max(ans, r - l);
255+
l = r;
256+
} else {
257+
++l;
258+
}
259+
}
260+
return ans;
261+
}
262+
```
263+
155264
### **...**
156265

157266
```

solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/Solution.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
class Solution {
22
public:
33
int longestAlternatingSubarray(vector<int>& nums, int threshold) {
4-
int ans = 0, n = nums.size();
5-
for (int l = 0; l < n; ++l) {
4+
int ans = 0;
5+
for (int l = 0, n = nums.size(); l < n;) {
66
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
77
int r = l + 1;
88
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
99
++r;
1010
}
1111
ans = max(ans, r - l);
12+
l = r;
13+
} else {
14+
++l;
1215
}
1316
}
1417
return ans;
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
func longestAlternatingSubarray(nums []int, threshold int) int {
2-
ans, n := 0, len(nums)
3-
for l := range nums {
1+
func longestAlternatingSubarray(nums []int, threshold int) (ans int) {
2+
for l, n := 0, len(nums); l < n; {
43
if nums[l]%2 == 0 && nums[l] <= threshold {
54
r := l + 1
65
for r < n && nums[r]%2 != nums[r-1]%2 && nums[r] <= threshold {
76
r++
87
}
98
ans = max(ans, r-l)
9+
l = r
10+
} else {
11+
l++
1012
}
1113
}
12-
return ans
14+
return
1315
}

solution/2700-2799/2760.Longest Even Odd Subarray With Threshold/Solution.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
class Solution {
22
public int longestAlternatingSubarray(int[] nums, int threshold) {
3-
int ans = 0, n = nums.length;
4-
for (int l = 0; l < n; ++l) {
3+
int ans = 0;
4+
for (int l = 0, n = nums.length; l < n;) {
55
if (nums[l] % 2 == 0 && nums[l] <= threshold) {
66
int r = l + 1;
77
while (r < n && nums[r] % 2 != nums[r - 1] % 2 && nums[r] <= threshold) {
88
++r;
99
}
1010
ans = Math.max(ans, r - l);
11+
l = r;
12+
} else {
13+
++l;
1114
}
1215
}
1316
return ans;
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class Solution:
22
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
3-
ans, n = 0, len(nums)
4-
for l in range(n):
3+
ans, l, n = 0, 0, len(nums)
4+
while l < n:
55
if nums[l] % 2 == 0 and nums[l] <= threshold:
66
r = l + 1
77
while r < n and nums[r] % 2 != nums[r - 1] % 2 and nums[r] <= threshold:
88
r += 1
99
ans = max(ans, r - l)
10+
l = r
11+
else:
12+
l += 1
1013
return ans

0 commit comments

Comments
 (0)