Skip to content

feat: add solutions to lc problem: No.2900 #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,86 @@

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

**方法一:贪心 + 一次遍历**

我们可以遍历数组 $groups$,对于当前遍历到的下标 $i$,如果 $i=0$ 或者 $groups[i] \neq groups[i - 1]$,我们就将 $words[i]$ 加入答案数组中。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $groups$ 的长度。

<!-- tabs:start -->

### **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<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
List<String> 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<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
vector<string> 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;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

### **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<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
List<String> 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<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
vector<string> 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;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
vector<string> ans;
for (int i = 0; i < n; ++i) {
if (i == 0 || groups[i] != groups[i - 1]) {
ans.emplace_back(words[i]);
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
List<String> ans = new ArrayList<>();
for (int i = 0; i < n; ++i) {
if (i == 0 || groups[i] != groups[i - 1]) {
ans.add(words[i]);
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -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]]
Original file line number Diff line number Diff line change
@@ -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;
}