Skip to content

Commit 06bda0b

Browse files
authored
Merge pull request #201 from LetMeFly666/2765
添加了问题“2765.最长交替子数组”的代码和题解
2 parents c9ca600 + d252717 commit 06bda0b

File tree

5 files changed

+206
-3
lines changed

5 files changed

+206
-3
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
2-
title: 2765.最长交替子序列
2+
title: 2765.最长交替子数组
33
date: 2023-10-10 14-40-24
44
tags: [题解, LeetCode, 简单, 数组, 枚举]
55
---
66

7-
# 【LetMeFly】2765.最长交替子序列
7+
# 【LetMeFly】2765.最长交替子数组
88

99
力扣题目链接:[https://leetcode.cn/problems/longest-alternating-subarray/](https://leetcode.cn/problems/longest-alternating-subarray/)
1010

11-
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。如果 <code>nums</code>&nbsp;中长度为&nbsp;<code>m</code>&nbsp;的子数组&nbsp;<code>s</code>&nbsp;满足以下条件,我们称它是一个 <strong>交替子序列</strong> :</p>
11+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。如果 <code>nums</code>&nbsp;中长度为&nbsp;<code>m</code>&nbsp;的子数组&nbsp;<code>s</code>&nbsp;满足以下条件,我们称它是一个 <strong>交替子数组</strong> :</p>
1212

1313
<ul>
1414
<li><code>m</code>&nbsp;大于&nbsp;<code>1</code>&nbsp;。</li>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-01-23 21:58:59
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-01-23 22:21:13
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
int get1(vector<int>& nums, int oddLoc=1) {
14+
int evenLoc = -oddLoc;
15+
int ans = 1;
16+
int cnt = 1;
17+
for (int i = 0; i < nums.size(); i++) {
18+
int shouldAdd = i % 2 ? oddLoc : evenLoc;
19+
if (i + 1 == nums.size() || nums[i + 1] != nums[i] + shouldAdd || cnt == 1 && shouldAdd == -1) {
20+
ans = max(ans, cnt);
21+
cnt = 1;
22+
}
23+
else {
24+
cnt++;
25+
}
26+
}
27+
return ans;
28+
}
29+
public:
30+
int alternatingSubarray(vector<int>& nums) {
31+
int ans = max(get1(nums), get1(nums, -1));
32+
return ans < 2 ? -1 : ans;
33+
}
34+
};
35+
36+
#ifdef _WIN32
37+
/*
38+
[2,3,4,3,4]
39+
40+
4
41+
*/
42+
int main() {
43+
string s;
44+
while (cin >> s) {
45+
vector<int> v = stringToVector(s);
46+
Solution sol;
47+
cout << sol.alternatingSubarray(v) << endl;
48+
}
49+
return 0;
50+
}
51+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-01-23 22:22:14
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-01-23 22:25:36
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def get1(self, oddLoc=1) -> int:
11+
evenLoc = -oddLoc
12+
ans = 1
13+
cnt = 1
14+
for i in range(len(self.nums)):
15+
shouldAdd = oddLoc if i % 2 else evenLoc
16+
if i + 1 == len(self.nums) or self.nums[i + 1] != self.nums[i] + shouldAdd or (cnt == 1 and shouldAdd == -1):
17+
ans = max(ans, cnt)
18+
cnt = 1
19+
else:
20+
cnt += 1
21+
return ans
22+
23+
def alternatingSubarray(self, nums: List[int]) -> int:
24+
self.nums = nums
25+
ans = max(self.get1(), self.get1(-1))
26+
return ans if ans >= 2 else -1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@
522522
|2731.移动机器人|中等|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/10/LeetCode%202731.%E7%A7%BB%E5%8A%A8%E6%9C%BA%E5%99%A8%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133758255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/2476843/letmefly-2731yi-dong-ji-qi-ren-nao-jin-j-vwf2/" target="_blank">LeetCode题解</a>|
523523
|2744.最大字符串配对数目|简单|<a href="https://leetcode.cn/problems/find-maximum-number-of-string-pairs/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/17/LeetCode%202744.%E6%9C%80%E5%A4%A7%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%85%8D%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135662583" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-maximum-number-of-string-pairs/solutions/2608691/letmefly-2744zui-da-zi-fu-chuan-pei-dui-lnd3n/" target="_blank">LeetCode题解</a>|
524524
|2760.最长奇偶子数组|简单|<a href="https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/11/16/LeetCode%202760.%E6%9C%80%E9%95%BF%E5%A5%87%E5%81%B6%E5%AD%90%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134449952" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/2529749/letmefly-2760zui-chang-qi-ou-zi-shu-zu-m-f5ob/" target="_blank">LeetCode题解</a>|
525+
|2765.最长交替子数组|简单|<a href="https://leetcode.cn/problems/longest-alternating-subarray/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/23/LeetCode%202765.%E6%9C%80%E9%95%BF%E4%BA%A4%E6%9B%BF%E5%AD%90%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135794883" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-alternating-subarray/solutions/2617307/letmefly-2765zui-chang-jiao-ti-zi-shu-zu-tddr/" target="_blank">LeetCode题解</a>|
525526
|2788.按分隔符拆分字符串|简单|<a href="https://leetcode.cn/problems/split-strings-by-separator/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/20/LeetCode%202788.%E6%8C%89%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%86%E5%88%86%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135724019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-strings-by-separator/solutions/2612832/letmefly-2788an-fen-ge-fu-chai-fen-zi-fu-s1dn/" target="_blank">LeetCode题解</a>|
526527
|2807.在链表中插入最大公约数|中等|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/06/LeetCode%202807.%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%8F%92%E5%85%A5%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135424056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/2593002/letmefly-2807zai-lian-biao-zhong-cha-ru-idjs4/" target="_blank">LeetCode题解</a>|
527528
|2824.统计和小于目标的下标对数目|简单|<a href="https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/11/24/LeetCode%202824.%E7%BB%9F%E8%AE%A1%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%9B%AE%E6%A0%87%E7%9A%84%E4%B8%8B%E6%A0%87%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134594377" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/solutions/2539873/letmefly-2824tong-ji-he-xiao-yu-mu-biao-7ulk5/" target="_blank">LeetCode题解</a>|
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: 2765.最长交替子数组
3+
date: 2024-01-23 22:05:37
4+
tags: [题解, LeetCode, 简单, 数组, 枚举]
5+
---
6+
7+
# 【LetMeFly】2765.最长交替子数组:O(n)的做法(两次遍历)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/longest-alternating-subarray/](https://leetcode.cn/problems/longest-alternating-subarray/)
10+
11+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。如果 <code>nums</code>&nbsp;中长度为&nbsp;<code>m</code>&nbsp;的子数组&nbsp;<code>s</code>&nbsp;满足以下条件,我们称它是一个 <strong>交替子数组</strong> :</p>
12+
13+
<ul>
14+
<li><code>m</code>&nbsp;大于&nbsp;<code>1</code>&nbsp;。</li>
15+
<li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>&nbsp;。</li>
16+
<li>下标从 <strong>0</strong> 开始的子数组&nbsp;<code>s</code>&nbsp;与数组&nbsp;<code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>&nbsp;一样。也就是说,<code>s<sub>1</sub> - s<sub>0</sub> = 1</code>&nbsp;,<code>s<sub>2</sub> - s<sub>1</sub> = -1</code>&nbsp;,<code>s<sub>3</sub> - s<sub>2</sub> = 1</code>&nbsp;,<code>s<sub>4</sub> - s<sub>3</sub> = -1</code>&nbsp;,以此类推,直到&nbsp;<code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>&nbsp;。</li>
17+
</ul>
18+
19+
<p>请你返回 <code>nums</code>&nbsp;中所有 <strong>交替</strong>&nbsp;子数组中,最长的长度,如果不存在交替子数组,请你返回 <code>-1</code>&nbsp;。</p>
20+
21+
<p>子数组是一个数组中一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>示例 1:</strong></p>
26+
27+
<pre>
28+
<b>输入:</b>nums = [2,3,4,3,4]
29+
<b>输出:</b>4
30+
<b>解释:</b>交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre>
36+
<b>输入:</b>nums = [4,5,6]
37+
<b>输出:</b>2
38+
<strong>解释:</strong>[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
48+
</ul>
49+
50+
51+
## 方法零:O(n^2)的做法(两层循环)
52+
53+
第一层循环遍历“交替数组”的起点,第二层循环从这个起点开始往后遍历,得到交替数组的终点。更新答案的最大值。
54+
55+
## 方法一:O(n)的做法(两次遍历)
56+
57+
对于样例```[2, 3, 4, 3, 4]```,我们不能将```3```分给```2 3```,而是要把```3```分给```3 4 3 4```
58+
59+
怎么办呢?其实“交替数组”一共有两种:从奇数下标开始的数组和从偶数下标开始的数组。
60+
61+
因此,我们写一个函数来求“交替数组”,参数为“奇数下标时下一个元素该加一还是减一”。
62+
63+
求完两种交替数组的最大值,取二者最大的那个即为答案。
64+
65+
+ 时间复杂度$O(n)$,其中$n=len(nums)$
66+
+ 空间复杂度$O(1)$
67+
68+
### AC代码
69+
70+
#### C++
71+
72+
```cpp
73+
class Solution {
74+
private:
75+
int get1(vector<int>& nums, int oddLoc=1) {
76+
int evenLoc = -oddLoc;
77+
int ans = 1;
78+
int cnt = 1;
79+
for (int i = 0; i < nums.size(); i++) {
80+
int shouldAdd = i % 2 ? oddLoc : evenLoc;
81+
if (i + 1 == nums.size() || nums[i + 1] != nums[i] + shouldAdd || cnt == 1 && shouldAdd == -1) {
82+
ans = max(ans, cnt);
83+
cnt = 1;
84+
}
85+
else {
86+
cnt++;
87+
}
88+
}
89+
return ans;
90+
}
91+
public:
92+
int alternatingSubarray(vector<int>& nums) {
93+
int ans = max(get1(nums), get1(nums, -1));
94+
return ans < 2 ? -1 : ans;
95+
}
96+
};
97+
```
98+
99+
#### Python
100+
101+
```python
102+
# from typing import List
103+
104+
class Solution:
105+
def get1(self, oddLoc=1) -> int:
106+
evenLoc = -oddLoc
107+
ans = 1
108+
cnt = 1
109+
for i in range(len(self.nums)):
110+
shouldAdd = oddLoc if i % 2 else evenLoc
111+
if i + 1 == len(self.nums) or self.nums[i + 1] != self.nums[i] + shouldAdd or (cnt == 1 and shouldAdd == -1):
112+
ans = max(ans, cnt)
113+
cnt = 1
114+
else:
115+
cnt += 1
116+
return ans
117+
118+
def alternatingSubarray(self, nums: List[int]) -> int:
119+
self.nums = nums
120+
ans = max(self.get1(), self.get1(-1))
121+
return ans if ans >= 2 else -1
122+
```
123+
124+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/23/LeetCode%202765.%E6%9C%80%E9%95%BF%E4%BA%A4%E6%9B%BF%E5%AD%90%E6%95%B0%E7%BB%84/)哦~
125+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/135794883](https://letmefly.blog.csdn.net/article/details/135794883)

0 commit comments

Comments
 (0)