Skip to content

Commit 1ccacfa

Browse files
committed
feat: update solutions to lc problems: No.2900,2901
1 parent b6b8817 commit 1ccacfa

File tree

17 files changed

+96
-286
lines changed

17 files changed

+96
-286
lines changed

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md

+19-25
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,16 @@ tags:
8787

8888
```python
8989
class Solution:
90-
def getWordsInLongestSubsequence(
91-
self, n: int, words: List[str], groups: List[int]
92-
) -> List[str]:
90+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
9391
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
9492
```
9593

9694
#### Java
9795

9896
```java
9997
class Solution {
100-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
98+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
99+
int n = groups.length;
101100
List<String> ans = new ArrayList<>();
102101
for (int i = 0; i < n; ++i) {
103102
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -114,7 +113,8 @@ class Solution {
114113
```cpp
115114
class Solution {
116115
public:
117-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
116+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
117+
int n = groups.size();
118118
vector<string> ans;
119119
for (int i = 0; i < n; ++i) {
120120
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -129,22 +129,22 @@ public:
129129
#### Go
130130
131131
```go
132-
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
133-
for i, x := range groups {
134-
if i == 0 || x != groups[i-1] {
135-
ans = append(ans, words[i])
136-
}
137-
}
138-
return
132+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
133+
for i, x := range groups {
134+
if i == 0 || x != groups[i - 1] {
135+
ans = append(ans, words[i])
136+
}
137+
}
138+
return
139139
}
140140
```
141141

142142
#### TypeScript
143143

144144
```ts
145-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
145+
function getLongestSubsequence(words: string[], groups: number[]): string[] {
146146
const ans: string[] = [];
147-
for (let i = 0; i < n; ++i) {
147+
for (let i = 0; i < groups.length; ++i) {
148148
if (i === 0 || groups[i] !== groups[i - 1]) {
149149
ans.push(words[i]);
150150
}
@@ -157,19 +157,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
157157

158158
```rust
159159
impl Solution {
160-
pub fn get_words_in_longest_subsequence(
161-
n: i32,
162-
words: Vec<String>,
163-
groups: Vec<i32>,
164-
) -> Vec<String> {
165-
let mut ans = vec![];
166-
167-
for i in 0..n {
168-
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
169-
ans.push(words[i as usize].clone());
160+
pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
161+
let mut ans = Vec::new();
162+
for (i, &g) in groups.iter().enumerate() {
163+
if i == 0 || g != groups[i - 1] {
164+
ans.push(words[i].clone());
170165
}
171166
}
172-
173167
ans
174168
}
175169
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md

+19-25
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,16 @@ The time complexity is $O(n)$, where $n$ is the length of the array $groups$. Th
111111

112112
```python
113113
class Solution:
114-
def getWordsInLongestSubsequence(
115-
self, n: int, words: List[str], groups: List[int]
116-
) -> List[str]:
114+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
117115
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
118116
```
119117

120118
#### Java
121119

122120
```java
123121
class Solution {
124-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
122+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
123+
int n = groups.length;
125124
List<String> ans = new ArrayList<>();
126125
for (int i = 0; i < n; ++i) {
127126
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -138,7 +137,8 @@ class Solution {
138137
```cpp
139138
class Solution {
140139
public:
141-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
140+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
141+
int n = groups.size();
142142
vector<string> ans;
143143
for (int i = 0; i < n; ++i) {
144144
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -153,22 +153,22 @@ public:
153153
#### Go
154154
155155
```go
156-
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
157-
for i, x := range groups {
158-
if i == 0 || x != groups[i-1] {
159-
ans = append(ans, words[i])
160-
}
161-
}
162-
return
156+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
157+
for i, x := range groups {
158+
if i == 0 || x != groups[i - 1] {
159+
ans = append(ans, words[i])
160+
}
161+
}
162+
return
163163
}
164164
```
165165

166166
#### TypeScript
167167

168168
```ts
169-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
169+
function getLongestSubsequence(words: string[], groups: number[]): string[] {
170170
const ans: string[] = [];
171-
for (let i = 0; i < n; ++i) {
171+
for (let i = 0; i < groups.length; ++i) {
172172
if (i === 0 || groups[i] !== groups[i - 1]) {
173173
ans.push(words[i]);
174174
}
@@ -181,19 +181,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
181181

182182
```rust
183183
impl Solution {
184-
pub fn get_words_in_longest_subsequence(
185-
n: i32,
186-
words: Vec<String>,
187-
groups: Vec<i32>,
188-
) -> Vec<String> {
189-
let mut ans = vec![];
190-
191-
for i in 0..n {
192-
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
193-
ans.push(words[i as usize].clone());
184+
pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
185+
let mut ans = Vec::new();
186+
for (i, &g) in groups.iter().enumerate() {
187+
if i == 0 || g != groups[i - 1] {
188+
ans.push(words[i].clone());
194189
}
195190
}
196-
197191
ans
198192
}
199193
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution {
22
public:
3-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
3+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
4+
int n = groups.size();
45
vector<string> ans;
56
for (int i = 0; i < n; ++i) {
67
if (i == 0 || groups[i] != groups[i - 1]) {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
2-
for i, x := range groups {
3-
if i == 0 || x != groups[i-1] {
4-
ans = append(ans, words[i])
5-
}
6-
}
7-
return
1+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
2+
for i, x := range groups {
3+
if i == 0 || x != groups[i - 1] {
4+
ans = append(ans, words[i])
5+
}
6+
}
7+
return
88
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Solution {
2-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
2+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
3+
int n = groups.length;
34
List<String> ans = new ArrayList<>();
45
for (int i = 0; i < n; ++i) {
56
if (i == 0 || groups[i] != groups[i - 1]) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
class Solution:
2-
def getWordsInLongestSubsequence(
3-
self, n: int, words: List[str], groups: List[int]
4-
) -> List[str]:
2+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
53
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
impl Solution {
2-
pub fn get_words_in_longest_subsequence(
3-
n: i32,
4-
words: Vec<String>,
5-
groups: Vec<i32>,
6-
) -> Vec<String> {
7-
let mut ans = vec![];
8-
9-
for i in 0..n {
10-
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
11-
ans.push(words[i as usize].clone());
2+
pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
3+
let mut ans = Vec::new();
4+
for (i, &g) in groups.iter().enumerate() {
5+
if i == 0 || g != groups[i - 1] {
6+
ans.push(words[i].clone());
127
}
138
}
14-
159
ans
1610
}
1711
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
1+
function getLongestSubsequence(words: string[], groups: number[]): string[] {
22
const ans: string[] = [];
3-
for (let i = 0; i < n; ++i) {
3+
for (let i = 0; i < groups.length; ++i) {
44
if (i === 0 || groups[i] !== groups[i - 1]) {
55
ans.push(words[i]);
66
}

0 commit comments

Comments
 (0)