Skip to content

Commit ff306fb

Browse files
committed
update: 添加问题“2900.最长相邻不相等子序列I”的代码和题解(#938)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent c455794 commit ff306fb

7 files changed

+250
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-15 10:32:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-15 10:36:32
6+
* @Description: AC,100.00%,97.33%
7+
*/
8+
#if defined(_WIN32) || defined(__APPLE__)
9+
#include "_[1,2]toVector.h"
10+
#endif
11+
12+
class Solution {
13+
public:
14+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
15+
vector<string> ans;
16+
for (int i = 0; i < groups.size(); i++) {
17+
if (!i || groups[i] != groups[i - 1]) {
18+
ans.push_back(words[i]);
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-15 10:32:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-15 13:23:52
6+
*/
7+
package main
8+
9+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
10+
for i, g := range groups {
11+
if i == 0 || g != groups[i - 1] {
12+
ans = append(ans, words[i])
13+
}
14+
}
15+
return
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-15 10:32:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-15 13:22:29
6+
*/
7+
import java.util.List;
8+
import java.util.ArrayList;
9+
10+
class Solution {
11+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
12+
List<String> ans = new ArrayList<>();
13+
for (int i = 0; i < groups.length; i++) {
14+
if (i == 0 || groups[i] != groups[i - 1]) {
15+
ans.add(words[i]);
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-15 10:32:15
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-15 13:21:42
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
11+
ans = []
12+
for i, g in enumerate(groups):
13+
if not i or g != groups[i - 1]:
14+
ans.append(words[i])
15+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@
866866
|2859.计算K置位下标对应元素的和|简单|<a href="https://leetcode.cn/problems/sum-of-values-at-indices-with-k-set-bits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/25/LeetCode%202859.%E8%AE%A1%E7%AE%97K%E7%BD%AE%E4%BD%8D%E4%B8%8B%E6%A0%87%E5%AF%B9%E5%BA%94%E5%85%83%E7%B4%A0%E7%9A%84%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135836080" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-values-at-indices-with-k-set-bits/solutions/2618937/letmefly-2859ji-suan-k-zhi-wei-xia-biao-usyx1/" target="_blank">LeetCode题解</a>|
867867
|2860.让所有学生保持开心的分组方法数|中等|<a href="https://leetcode.cn/problems/happy-students/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/04/LeetCode%202860.%E8%AE%A9%E6%89%80%E6%9C%89%E5%AD%A6%E7%94%9F%E4%BF%9D%E6%8C%81%E5%BC%80%E5%BF%83%E7%9A%84%E5%88%86%E7%BB%84%E6%96%B9%E6%B3%95%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141905408" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/happy-students/solutions/2905023/letmefly-2860rang-suo-you-xue-sheng-bao-t8u1o/" target="_blank">LeetCode题解</a>|
868868
|2864.最大二进制奇数|简单|<a href="https://leetcode.cn/problems/maximum-odd-binary-number/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/13/LeetCode%202864.%E6%9C%80%E5%A4%A7%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%A5%87%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136669293" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-odd-binary-number/solutions/2683729/letmefly-2864zui-da-er-jin-zhi-qi-shu-ta-30tq/" target="_blank">LeetCode题解</a>|
869+
|2900.最长相邻不相等子序列I|简单|<a href="https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/15/LeetCode%202900.%E6%9C%80%E9%95%BF%E7%9B%B8%E9%82%BB%E4%B8%8D%E7%9B%B8%E7%AD%89%E5%AD%90%E5%BA%8F%E5%88%97I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147990003" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/solutions/3677963/letmefly-2900zui-chang-xiang-lin-bu-xian-69ua/" target="_blank">LeetCode题解</a>|
869870
|2903.找出满足差值条件的下标I|简单|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/25/LeetCode%202903.%E6%89%BE%E5%87%BA%E6%BB%A1%E8%B6%B3%E5%B7%AE%E5%80%BC%E6%9D%A1%E4%BB%B6%E7%9A%84%E4%B8%8B%E6%A0%87I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139195914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/solutions/2789520/letmefly-2903zhao-chu-man-zu-chai-zhi-ti-m92q/" target="_blank">LeetCode题解</a>|
870871
|2908.元素和最小的山形三元组I|简单|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/29/LeetCode%202908.%E5%85%83%E7%B4%A0%E5%92%8C%E6%9C%80%E5%B0%8F%E7%9A%84%E5%B1%B1%E5%BD%A2%E4%B8%89%E5%85%83%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137151595" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/solutions/2714346/letmefly-2908yuan-su-he-zui-xiao-de-shan-h3s9/" target="_blank">LeetCode题解</a>|
871872
|2917.找出数组中的K-or值|简单|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/06/LeetCode%202917.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84K-or%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136497896" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/solutions/2670279/letmefly-2917zhao-chu-shu-zu-zhong-de-k-4wuuh/" target="_blank">LeetCode题解</a>|
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
title: 2900.最长相邻不相等子序列 I:阅读理解题——O(n)一次遍历(贪心)
3+
date: 2025-05-15 13:27:30
4+
tags: [题解, LeetCode, 简单, 贪心, 数组, 字符串, 动态规划]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2900.最长相邻不相等子序列 I:阅读理解题——O(n)一次遍历(贪心)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/](https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/)
11+
12+
<p>给你一个下标从&nbsp;<strong>0</strong>&nbsp;开始的字符串数组&nbsp;<code>words</code>&nbsp;,和一个下标从 <strong>0</strong>&nbsp;开始的 <strong>二进制</strong>&nbsp;数组&nbsp;<code>groups</code>&nbsp;,两个数组长度都是&nbsp;<code>n</code>&nbsp;。</p>
13+
14+
<p>你需要从&nbsp;<code>words</code>&nbsp;中选出&nbsp;<strong>最长<span data-keyword="subsequence-array">子序列</span></strong>。如果对于序列中的任何两个连续串,二进制数组&nbsp;<code>groups</code>&nbsp;中它们的对应元素不同,则&nbsp;<code>words</code> 的子序列是不同的。</p>
15+
16+
<p>正式来说,你需要从下标&nbsp;<code>[0, 1, ..., n - 1]</code>&nbsp;中选出一个&nbsp;<strong>最长子序列</strong>&nbsp;,将这个子序列记作长度为 <code>k</code> 的&nbsp;<code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code>&nbsp;,对于所有满足&nbsp;<code>0 &lt;= j&nbsp;&lt; k - 1</code>&nbsp;&nbsp;<code>j</code>&nbsp;都有&nbsp;<code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>&nbsp;。</p>
17+
18+
<p>请你返回一个字符串数组,它是下标子序列&nbsp;<strong>依次</strong>&nbsp;对应&nbsp;<code>words</code>&nbsp;数组中的字符串连接形成的字符串数组。如果有多个答案,返回 <strong>任意</strong> 一个。</p>
19+
20+
<p><b>注意:</b><code>words</code>&nbsp;中的元素是不同的&nbsp;。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<pre>
27+
<b>输入:</b>words = ["e","a","b"], groups = [0,0,1]
28+
<b>输出:</b>["e","b"]
29+
<strong>解释:</strong>一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。
30+
所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。
31+
另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。
32+
得到答案为 [words[1],words[2]] = ["a","b"] 。
33+
这也是一个可行的答案。
34+
符合题意的最长子序列的长度为 2 。</pre>
35+
36+
<p><strong class="example">示例 2:</strong></p>
37+
38+
<pre>
39+
<b>输入:</b>words = ["a","b","c","d"], groups = [1,0,1,1]
40+
<b>输出:</b>["a","b","c"]
41+
<b>解释:</b>一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。
42+
所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。
43+
另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。
44+
得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。
45+
这也是一个可行的答案。
46+
符合题意的最长子序列的长度为 3 。</pre>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>提示:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
54+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
55+
<li><code>groups[i]</code>&nbsp;是&nbsp;<code>0</code>&nbsp;或&nbsp;<code>1</code>。</li>
56+
<li><code>words</code>&nbsp;中的字符串 <strong>互不相同</strong>&nbsp;。</li>
57+
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
58+
</ul>
59+
60+
61+
62+
## 解题方法:贪心
63+
64+
这道题描述得很复杂,大概是为了给II做铺垫。读懂题意了倒是也很简单:
65+
66+
> 先不管words数组,只看groups数组。在groups数组中选一些元素使得挑选结果为`0101..``101010...`
67+
>
68+
> 想让挑选的元素尽可能地多,最终返回挑选元素对应下标在words中对应的字符串们。
69+
70+
怎么挑?贪心,能选就选呗。
71+
72+
一次遍历groups数组,若当前元素和上一个元素不同,则挑选之。
73+
74+
+ 时间复杂度$O(len(groups))$
75+
+ 空间复杂度$O(1)$,力扣返回值不计入算法空间复杂度
76+
77+
### AC代码
78+
79+
#### C++
80+
81+
```cpp
82+
/*
83+
* @Author: LetMeFly
84+
* @Date: 2025-05-15 10:32:15
85+
* @LastEditors: LetMeFly.xyz
86+
* @LastEditTime: 2025-05-15 10:36:32
87+
* @Description: AC,100.00%,97.33%
88+
*/
89+
class Solution {
90+
public:
91+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
92+
vector<string> ans;
93+
for (int i = 0; i < groups.size(); i++) {
94+
if (!i || groups[i] != groups[i - 1]) {
95+
ans.push_back(words[i]);
96+
}
97+
}
98+
return ans;
99+
}
100+
};
101+
```
102+
103+
#### Python
104+
105+
```python
106+
'''
107+
Author: LetMeFly
108+
Date: 2025-05-15 10:32:15
109+
LastEditors: LetMeFly.xyz
110+
LastEditTime: 2025-05-15 13:21:42
111+
'''
112+
from typing import List
113+
114+
class Solution:
115+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
116+
ans = []
117+
for i, g in enumerate(groups):
118+
if not i or g != groups[i - 1]:
119+
ans.append(words[i])
120+
return ans
121+
```
122+
123+
#### Java
124+
125+
```java
126+
/*
127+
* @Author: LetMeFly
128+
* @Date: 2025-05-15 10:32:15
129+
* @LastEditors: LetMeFly.xyz
130+
* @LastEditTime: 2025-05-15 13:22:29
131+
*/
132+
import java.util.List;
133+
import java.util.ArrayList;
134+
135+
class Solution {
136+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
137+
List<String> ans = new ArrayList<>();
138+
for (int i = 0; i < groups.length; i++) {
139+
if (i == 0 || groups[i] != groups[i - 1]) {
140+
ans.add(words[i]);
141+
}
142+
}
143+
return ans;
144+
}
145+
}
146+
```
147+
148+
#### Go
149+
150+
```go
151+
/*
152+
* @Author: LetMeFly
153+
* @Date: 2025-05-15 10:32:15
154+
* @LastEditors: LetMeFly.xyz
155+
* @LastEditTime: 2025-05-15 13:23:52
156+
*/
157+
package main
158+
159+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
160+
for i, g := range groups {
161+
if i == 0 || g != groups[i - 1] {
162+
ans = append(ans, words[i])
163+
}
164+
}
165+
return
166+
}
167+
```
168+
169+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147990003)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/05/15/LeetCode%202900.%E6%9C%80%E9%95%BF%E7%9B%B8%E9%82%BB%E4%B8%8D%E7%9B%B8%E7%AD%89%E5%AD%90%E5%BA%8F%E5%88%97I/)哦~
170+
>
171+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,10 @@ categories: [自用]
11531153
|sexuality|n. 性欲,性倾向|
11541154
|||
11551155
|oversight|n. 监督,照管;疏忽,失察,忽略,负责|
1156+
|||
1157+
|masculine|adj. 男人的,像男人的,阳性的<br/>n. 阳性|
1158+
|liability|n. 责任,义务,不利因素,欠债,负债,可能性|
1159+
|tactics|n. 战术,策略|
11561160

11571161
<p class="wordCounts">单词收录总数</p>
11581162

0 commit comments

Comments
 (0)