|
| 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>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> ,和一个下标从 <strong>0</strong> 开始的 <strong>二进制</strong> 数组 <code>groups</code> ,两个数组长度都是 <code>n</code> 。</p> |
| 13 | + |
| 14 | +<p>你需要从 <code>words</code> 中选出 <strong>最长<span data-keyword="subsequence-array">子序列</span></strong>。如果对于序列中的任何两个连续串,二进制数组 <code>groups</code> 中它们的对应元素不同,则 <code>words</code> 的子序列是不同的。</p> |
| 15 | + |
| 16 | +<p>正式来说,你需要从下标 <code>[0, 1, ..., n - 1]</code> 中选出一个 <strong>最长子序列</strong> ,将这个子序列记作长度为 <code>k</code> 的 <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> ,对于所有满足 <code>0 <= j < k - 1</code> 的 <code>j</code> 都有 <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code> 。</p> |
| 17 | + |
| 18 | +<p>请你返回一个字符串数组,它是下标子序列 <strong>依次</strong> 对应 <code>words</code> 数组中的字符串连接形成的字符串数组。如果有多个答案,返回 <strong>任意</strong> 一个。</p> |
| 19 | + |
| 20 | +<p><b>注意:</b><code>words</code> 中的元素是不同的 。</p> |
| 21 | + |
| 22 | +<p> </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> </p> |
| 49 | + |
| 50 | +<p><strong>提示:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= n == words.length == groups.length <= 100</code></li> |
| 54 | + <li><code>1 <= words[i].length <= 10</code></li> |
| 55 | + <li><code>groups[i]</code> 是 <code>0</code> 或 <code>1</code>。</li> |
| 56 | + <li><code>words</code> 中的字符串 <strong>互不相同</strong> 。</li> |
| 57 | + <li><code>words[i]</code> 只包含小写英文字母。</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) |
0 commit comments