Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d73cb2

Browse files
feat: add solutions to lc problem: No.2900 (doocs#1810)
No.2900.Longest Unequal Adjacent Groups Subsequence I
1 parent 118cf79 commit 2d73cb2

File tree

7 files changed

+155
-6
lines changed

7 files changed

+155
-6
lines changed

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,86 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:贪心 + 一次遍历**
62+
63+
我们可以遍历数组 $groups,ドル对于当前遍历到的下标 $i,ドル如果 $i=0$ 或者 $groups[i] \neq groups[i - 1],ドル我们就将 $words[i]$ 加入答案数组中。
64+
65+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $groups$ 的长度。
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
6470

6571
<!-- 这里可写当前语言的特殊实现逻辑 -->
6672

6773
```python
68-
74+
class Solution:
75+
def getWordsInLongestSubsequence(
76+
self, n: int, words: List[str], groups: List[int]
77+
) -> List[str]:
78+
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
6979
```
7080

7181
### **Java**
7282

7383
<!-- 这里可写当前语言的特殊实现逻辑 -->
7484

7585
```java
76-
86+
class Solution {
87+
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
88+
List<String> ans = new ArrayList<>();
89+
for (int i = 0; i < n; ++i) {
90+
if (i == 0 || groups[i] != groups[i - 1]) {
91+
ans.add(words[i]);
92+
}
93+
}
94+
return ans;
95+
}
96+
}
7797
```
7898

7999
### **C++**
80100

81101
```cpp
82-
102+
class Solution {
103+
public:
104+
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
105+
vector<string> ans;
106+
for (int i = 0; i < n; ++i) {
107+
if (i == 0 || groups[i] != groups[i - 1]) {
108+
ans.emplace_back(words[i]);
109+
}
110+
}
111+
return ans;
112+
}
113+
};
83114
```
84115
85116
### **Go**
86117
87118
```go
119+
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
120+
for i, x := range groups {
121+
if i == 0 || x != groups[i-1] {
122+
ans = append(ans, words[i])
123+
}
124+
}
125+
return
126+
}
127+
```
88128

129+
### **TypeScript**
130+
131+
```ts
132+
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
133+
const ans: string[] = [];
134+
for (let i = 0; i < n; ++i) {
135+
if (i === 0 || groups[i] !== groups[i - 1]) {
136+
ans.push(words[i]);
137+
}
138+
}
139+
return ans;
140+
}
89141
```
90142

91143
### **...**

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,82 @@ It can be shown that the length of the longest subsequence of indices that satis
5353

5454
## Solutions
5555

56+
**Solution 1: Greedy**
57+
58+
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.
59+
60+
The time complexity is $O(n),ドル where $n$ is the length of the array $groups$. The space complexity is $O(n)$.
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
```python
61-
67+
class Solution:
68+
def getWordsInLongestSubsequence(
69+
self, n: int, words: List[str], groups: List[int]
70+
) -> List[str]:
71+
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
6272
```
6373

6474
### **Java**
6575

6676
```java
67-
77+
class Solution {
78+
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
79+
List<String> ans = new ArrayList<>();
80+
for (int i = 0; i < n; ++i) {
81+
if (i == 0 || groups[i] != groups[i - 1]) {
82+
ans.add(words[i]);
83+
}
84+
}
85+
return ans;
86+
}
87+
}
6888
```
6989

7090
### **C++**
7191

7292
```cpp
73-
93+
class Solution {
94+
public:
95+
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
96+
vector<string> ans;
97+
for (int i = 0; i < n; ++i) {
98+
if (i == 0 || groups[i] != groups[i - 1]) {
99+
ans.emplace_back(words[i]);
100+
}
101+
}
102+
return ans;
103+
}
104+
};
74105
```
75106
76107
### **Go**
77108
78109
```go
110+
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
111+
for i, x := range groups {
112+
if i == 0 || x != groups[i-1] {
113+
ans = append(ans, words[i])
114+
}
115+
}
116+
return
117+
}
118+
```
79119

120+
### **TypeScript**
121+
122+
```ts
123+
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
124+
const ans: string[] = [];
125+
for (let i = 0; i < n; ++i) {
126+
if (i === 0 || groups[i] !== groups[i - 1]) {
127+
ans.push(words[i]);
128+
}
129+
}
130+
return ans;
131+
}
80132
```
81133

82134
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
4+
vector<string> ans;
5+
for (int i = 0; i < n; ++i) {
6+
if (i == 0 || groups[i] != groups[i - 1]) {
7+
ans.emplace_back(words[i]);
8+
}
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
3+
List<String> ans = new ArrayList<>();
4+
for (int i = 0; i < n; ++i) {
5+
if (i == 0 || groups[i] != groups[i - 1]) {
6+
ans.add(words[i]);
7+
}
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def getWordsInLongestSubsequence(
3+
self, n: int, words: List[str], groups: List[int]
4+
) -> List[str]:
5+
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
2+
const ans: string[] = [];
3+
for (let i = 0; i < n; ++i) {
4+
if (i === 0 || groups[i] !== groups[i - 1]) {
5+
ans.push(words[i]);
6+
}
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /