From d73e2ca8d2979722a60091355b190e8ce1652b91 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 15 May 2025 09:18:47 +0800 Subject: [PATCH] Add solution and test-cases for problem 2900 --- .../README.md | 39 +++++++++++++++++++ .../Solution.go | 27 +++++++++++++ .../Solution_test.go | 39 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/README.md create mode 100755 leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution.go create mode 100755 leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution_test.go diff --git a/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/README.md b/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/README.md new file mode 100644 index 000000000..8138d3f11 --- /dev/null +++ b/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/README.md @@ -0,0 +1,39 @@ +# [2900.Longest Unequal Adjacent Groups Subsequence I][title] + +## Description +You are given a string array `words` and a **binary** array `groups` both of length `n`, where `words[i]` is associated with `groups[i]`. + +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. + +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. + +Return the selected subsequence. If there are multiple answers, return **any** of them. + +**Note**: The elements in `words` are distinct. + +**Example 1:** + +``` +Input: words = ["e","a","b"], groups = [0,0,1] + +Output: ["e","b"] + +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. +``` + +**Example 2:** + +``` +Input: words = ["a","b","c","d"], groups = [1,0,1,1] + +Output: ["a","b","c"] + +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. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution.go b/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution.go new file mode 100755 index 000000000..d4dfe0cc6 --- /dev/null +++ b/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution.go @@ -0,0 +1,27 @@ +package Solution + +func Solution(words []string, groups []int) []string { + l, index := 1, 0 + dp := make([][2]int, len(words)) + dp[0] = [2]int{1, -1} // 前一个 + for i := 1; i < len(words); i++ { + dp[i] = [2]int{1, -1} + for pre := i - 1; pre >= 0; pre-- { + if groups[i] != groups[pre] { + if dp[pre][0]+1 > dp[i][0] { + dp[i] = [2]int{dp[pre][0] + 1, pre} + } + } + } + if dp[i][0] > l { + l = dp[i][0] + index = i + } + } + ans := make([]string, l) + for i := l - 1; i >= 0; i-- { + ans[i] = words[index] + index = dp[index][1] + } + return ans +} diff --git a/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution_test.go b/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution_test.go new file mode 100755 index 000000000..b6f1eb8d4 --- /dev/null +++ b/leetcode/2801-2900/2900.Longest-Unequal-Adjacent-Groups-Subsequence-I/Solution_test.go @@ -0,0 +1,39 @@ +package Solution + +import ( + "reflect" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + words []string + groups []int + expect []string + }{ + {"TestCase1", []string{"e", "a", "b"}, []int{0, 0, 1}, []string{"a", "b"}}, + {"TestCase2", []string{"a", "b", "c", "d"}, []int{1, 0, 1, 1}, []string{"a", "b", "c"}}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.words, c.groups) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.words, c.groups) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}