LeetCode 2900.最长相邻不相等子序列 I:阅读理解题——O(n)一次遍历(贪心)
【LetMeFly】2900.最长相邻不相等子序列 I:阅读理解题——O(n)一次遍历(贪心)
力扣题目链接:https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/
给你一个下标从 0 开始的字符串数组 words
,和一个下标从 0 开始的 二进制 数组 groups
,两个数组长度都是 n
。
你需要从 words
中选出 最长子序列。如果对于序列中的任何两个连续串,二进制数组 groups
中它们的对应元素不同,则 words
的子序列是不同的。
正式来说,你需要从下标 [0, 1, ..., n - 1]
中选出一个 最长子序列 ,将这个子序列记作长度为 k
的 [i0, i1, ..., ik - 1]
,对于所有满足 0 <= j < k - 1
的 j
都有 groups[ij] != groups[ij + 1]
。
请你返回一个字符串数组,它是下标子序列 依次 对应 words
数组中的字符串连接形成的字符串数组。如果有多个答案,返回 任意 一个。
注意:words
中的元素是不同的 。
示例 1:
输入:words = ["e","a","b"], groups = [0,0,1] 输出:["e","b"] 解释:一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。 所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。 另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。 得到答案为 [words[1],words[2]] = ["a","b"] 。 这也是一个可行的答案。 符合题意的最长子序列的长度为 2 。
示例 2:
输入:words = ["a","b","c","d"], groups = [1,0,1,1] 输出:["a","b","c"] 解释:一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。 所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。 另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。 得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。 这也是一个可行的答案。 符合题意的最长子序列的长度为 3 。
提示:
1 <= n == words.length == groups.length <= 100
1 <= words[i].length <= 10
groups[i]
是0
或1
。words
中的字符串 互不相同 。words[i]
只包含小写英文字母。
解题方法:贪心
这道题描述得很复杂,大概是为了给II做铺垫。读懂题意了倒是也很简单:
先不管words数组,只看groups数组。在groups数组中选一些元素使得挑选结果为
0101..
或101010...
。想让挑选的元素尽可能地多,最终返回挑选元素对应下标在words中对应的字符串们。
怎么挑?贪心,能选就选呗。
一次遍历groups数组,若当前元素和上一个元素不同,则挑选之。
- 时间复杂度 O ( l e n ( g r o u p s ) ) O(len(groups)) O(len(groups))
- 空间复杂度 O ( 1 ) O(1) O(1),力扣返回值不计入算法空间复杂度
AC代码
C++
/** @Author: LetMeFly* @Date: 2025-05-15 10:32:15* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-05-15 10:36:32* @Description: AC,100.00%,97.33%*/
class Solution {
public:vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {vector<string> ans;for (int i = 0; i < groups.size(); i++) {if (!i || groups[i] != groups[i - 1]) {ans.push_back(words[i]);}}return ans;}
};
Python
'''
Author: LetMeFly
Date: 2025-05-15 10:32:15
LastEditors: LetMeFly.xyz
LastEditTime: 2025-05-15 13:21:42
'''
from typing import Listclass Solution:def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:ans = []for i, g in enumerate(groups):if not i or g != groups[i - 1]:ans.append(words[i])return ans
Java
/** @Author: LetMeFly* @Date: 2025-05-15 10:32:15* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-05-15 13:22:29*/
import java.util.List;
import java.util.ArrayList;class Solution {public List<String> getLongestSubsequence(String[] words, int[] groups) {List<String> ans = new ArrayList<>();for (int i = 0; i < groups.length; i++) {if (i == 0 || groups[i] != groups[i - 1]) {ans.add(words[i]);}}return ans;}
}
Go
/** @Author: LetMeFly* @Date: 2025-05-15 10:32:15* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-05-15 13:23:52*/
package mainfunc getLongestSubsequence(words []string, groups []int) (ans []string) {for i, g := range groups {if i == 0 || g != groups[i - 1] {ans = append(ans, words[i])}}return
}
同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
千篇源码题解已开源