Skip to content

Commit d73e2ca

Browse files
committed
Add solution and test-cases for problem 2900
1 parent a01e89c commit d73e2ca

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [2900.Longest Unequal Adjacent Groups Subsequence I][title]
2+
3+
## Description
4+
You are given a string array `words` and a **binary** array `groups` both of length `n`, where `words[i]` is associated with `groups[i]`.
5+
6+
Your task is to select the **longest alternating** subsequence from `words`. A subsequence of `words` is alternating if for any two consecutive strings in the sequence, their corresponding elements in the binary array `groups` differ. Essentially, you are to choose strings such that adjacent elements have non-matching corresponding bits in the `groups` array.
7+
8+
Formally, you need to find the longest subsequence of an array of indices `[0, 1, ..., n - 1]` denoted as `[i0, i1, ..., ik-1]`, such that `groups[ij] != groups[ij+1]` for each `0 <= j < k - 1` and then find the words corresponding to these indices.
9+
10+
Return the selected subsequence. If there are multiple answers, return **any** of them.
11+
12+
**Note**: The elements in `words` are distinct.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: words = ["e","a","b"], groups = [0,0,1]
18+
19+
Output: ["e","b"]
20+
21+
Explanation: A subsequence that can be selected is ["e","b"] because groups[0] != groups[2]. Another subsequence that can be selected is ["a","b"] because groups[1] != groups[2]. It can be demonstrated that the length of the longest subsequence of indices that satisfies the condition is 2.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: words = ["a","b","c","d"], groups = [1,0,1,1]
28+
29+
Output: ["a","b","c"]
30+
31+
Explanation: A subsequence that can be selected is ["a","b","c"] because groups[0] != groups[1] and groups[1] != groups[2]. Another subsequence that can be selected is ["a","b","d"] because groups[0] != groups[1] and groups[1] != groups[3]. It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
32+
```
33+
34+
## 结语
35+
36+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
37+
38+
[title]: https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i
39+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Solution
2+
3+
func Solution(words []string, groups []int) []string {
4+
l, index := 1, 0
5+
dp := make([][2]int, len(words))
6+
dp[0] = [2]int{1, -1} // 前一个
7+
for i := 1; i < len(words); i++ {
8+
dp[i] = [2]int{1, -1}
9+
for pre := i - 1; pre >= 0; pre-- {
10+
if groups[i] != groups[pre] {
11+
if dp[pre][0]+1 > dp[i][0] {
12+
dp[i] = [2]int{dp[pre][0] + 1, pre}
13+
}
14+
}
15+
}
16+
if dp[i][0] > l {
17+
l = dp[i][0]
18+
index = i
19+
}
20+
}
21+
ans := make([]string, l)
22+
for i := l - 1; i >= 0; i-- {
23+
ans[i] = words[index]
24+
index = dp[index][1]
25+
}
26+
return ans
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
words []string
14+
groups []int
15+
expect []string
16+
}{
17+
{"TestCase1", []string{"e", "a", "b"}, []int{0, 0, 1}, []string{"a", "b"}},
18+
{"TestCase2", []string{"a", "b", "c", "d"}, []int{1, 0, 1, 1}, []string{"a", "b", "c"}},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := Solution(c.words, c.groups)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.words, c.groups)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
}
36+
37+
// 使用案列
38+
func ExampleSolution() {
39+
}

0 commit comments

Comments
 (0)