diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md index 480c3633b7b44..764f0816c50f7 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md @@ -58,6 +58,12 @@ +**方法一:贪心 + 一次遍历** + +我们可以遍历数组 $groups$,对于当前遍历到的下标 $i$,如果 $i=0$ 或者 $groups[i] \neq groups[i - 1]$,我们就将 $words[i]$ 加入答案数组中。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $groups$ 的长度。 + ### **Python3** @@ -65,7 +71,11 @@ ```python - +class Solution: + def getWordsInLongestSubsequence( + self, n: int, words: List[str], groups: List[int] + ) -> List[str]: + return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]] ``` ### **Java** @@ -73,19 +83,61 @@ ```java - +class Solution { + public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + List ans = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + if (i == 0 || groups[i] != groups[i - 1]) { + ans.add(words[i]); + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector ans; + for (int i = 0; i < n; ++i) { + if (i == 0 || groups[i] != groups[i - 1]) { + ans.emplace_back(words[i]); + } + } + return ans; + } +}; ``` ### **Go** ```go +func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) { + for i, x := range groups { + if i == 0 || x != groups[i-1] { + ans = append(ans, words[i]) + } + } + return +} +``` +### **TypeScript** + +```ts +function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { + const ans: string[] = []; + for (let i = 0; i < n; ++i) { + if (i === 0 || groups[i] !== groups[i - 1]) { + ans.push(words[i]); + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md index 9c3e11ec73e4c..442c8b51ea454 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md @@ -53,30 +53,82 @@ It can be shown that the length of the longest subsequence of indices that satis ## Solutions +**Solution 1: Greedy** + +We can traverse the array $groups$, and for the current index $i$, if $i=0$ or $groups[i] \neq groups[i - 1]$, we add $words[i]$ to the answer array. + +The time complexity is $O(n)$, where $n$ is the length of the array $groups$. The space complexity is $O(n)$. + ### **Python3** ```python - +class Solution: + def getWordsInLongestSubsequence( + self, n: int, words: List[str], groups: List[int] + ) -> List[str]: + return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]] ``` ### **Java** ```java - +class Solution { + public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + List ans = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + if (i == 0 || groups[i] != groups[i - 1]) { + ans.add(words[i]); + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector ans; + for (int i = 0; i < n; ++i) { + if (i == 0 || groups[i] != groups[i - 1]) { + ans.emplace_back(words[i]); + } + } + return ans; + } +}; ``` ### **Go** ```go +func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) { + for i, x := range groups { + if i == 0 || x != groups[i-1] { + ans = append(ans, words[i]) + } + } + return +} +``` +### **TypeScript** + +```ts +function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { + const ans: string[] = []; + for (let i = 0; i < n; ++i) { + if (i === 0 || groups[i] !== groups[i - 1]) { + ans.push(words[i]); + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp new file mode 100644 index 0000000000000..c686de7e6c4d1 --- /dev/null +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector ans; + for (int i = 0; i < n; ++i) { + if (i == 0 || groups[i] != groups[i - 1]) { + ans.emplace_back(words[i]); + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go new file mode 100644 index 0000000000000..0fbd97a384c83 --- /dev/null +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go @@ -0,0 +1,8 @@ +func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) { + for i, x := range groups { + if i == 0 || x != groups[i-1] { + ans = append(ans, words[i]) + } + } + return +} \ No newline at end of file diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java new file mode 100644 index 0000000000000..9e3b87d016e6d --- /dev/null +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + List ans = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + if (i == 0 || groups[i] != groups[i - 1]) { + ans.add(words[i]); + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py new file mode 100644 index 0000000000000..250331a458490 --- /dev/null +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py @@ -0,0 +1,5 @@ +class Solution: + def getWordsInLongestSubsequence( + self, n: int, words: List[str], groups: List[int] + ) -> List[str]: + return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]] diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts new file mode 100644 index 0000000000000..71386aa0ab21c --- /dev/null +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts @@ -0,0 +1,9 @@ +function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { + const ans: string[] = []; + for (let i = 0; i < n; ++i) { + if (i === 0 || groups[i] !== groups[i - 1]) { + ans.push(words[i]); + } + } + return ans; +}