From 7cae70e3ccf356015283f16862cedecdeda1e886 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 15 Oct 2023 23:19:58 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2900 No.2900.Longest Unequal Adjacent Groups Subsequence I --- .../README.md | 58 ++++++++++++++++++- .../README_EN.md | 58 ++++++++++++++++++- .../Solution.cpp | 12 ++++ .../Solution.go | 8 +++ .../Solution.java | 11 ++++ .../Solution.py | 5 ++ .../Solution.ts | 9 +++ 7 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp create mode 100644 solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go create mode 100644 solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java create mode 100644 solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py create mode 100644 solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts 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..ad76994fe64d6 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..1d3e6bf083078 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..d23382e25848a --- /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; +} From a826535516a6c371856d5e695d21541061dd5dd4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 15 Oct 2023 23:33:43 +0800 Subject: [PATCH 2/2] fix: code style --- .../README.md | 2 +- .../README_EN.md | 2 +- .../Solution.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 ad76994fe64d6..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 @@ -108,7 +108,7 @@ public: ans.emplace_back(words[i]); } } - return ans; + 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 1d3e6bf083078..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 @@ -99,7 +99,7 @@ public: ans.emplace_back(words[i]); } } - return ans; + 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 index d23382e25848a..c686de7e6c4d1 100644 --- 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 @@ -7,6 +7,6 @@ class Solution { ans.emplace_back(words[i]); } } - return ans; + return ans; } }; \ No newline at end of file