Skip to content

Commit 6cb7828

Browse files
committed
#2900: Longest Unequal Adjacent Groups Subsequence I; solution & tests.
1 parent 0071e5a commit 6cb7828

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* 2900
3+
* Longest Unequal Adjacent Groups Subsequence I
4+
**
5+
* You are given a string array words and a binary array groups both of length n,
6+
* where words[i] is associated with groups[i].
7+
* Your task is to select the longest alternating subsequence from words.
8+
* A subsequence of words is alternating if for any two consecutive strings in the sequence,
9+
* their corresponding elements in the binary array groups differ.
10+
* Essentially,
11+
* you are to choose strings such that
12+
* adjacent elements have non-matching corresponding bits in the groups array.
13+
* Formally,
14+
* you need to find the longest subsequence of an array of indices
15+
* [0, 1, ..., n - 1] denoted as [i0, i1, ..., ik-1],
16+
* such that groups[ij] != groups[ij+1] for each 0 <= j < k - 1 and
17+
* then find the words corresponding to these indices.
18+
*
19+
* Return the selected subsequence.
20+
* If there are multiple answers,
21+
* return any of them.
22+
*
23+
* Note: The elements in words are distinct.
24+
*
25+
* Example 1:
26+
* Input: words = ["e","a","b"], groups = [0,0,1]
27+
* Output: ["e","b"]
28+
* Explanation:
29+
* A subsequence that can be selected is ["e","b"] because groups[0] != groups[2].
30+
* Another subsequence that can be selected is ["a","b"] because groups[1] != groups[2].
31+
* It can be demonstrated that
32+
* the length of the longest subsequence of indices that satisfies the condition is 2.
33+
*
34+
* Example 2:
35+
* Input: words = ["a","b","c","d"], groups = [1,0,1,1]
36+
* Output: ["a","b","c"]
37+
* Explanation:
38+
* A subsequence that can be selected is ["a","b","c"]
39+
* because groups[0] != groups[1] and groups[1] != groups[2].
40+
* Another subsequence that can be selected is ["a","b","d"]
41+
* because groups[0] != groups[1] and groups[1] != groups[3].
42+
* It can be shown that
43+
* the length of the longest subsequence of indices
44+
* that satisfies the condition is 3.
45+
*
46+
* Constraints:
47+
* • 1 <= n == words.length == groups.length <= 100
48+
* • 1 <= words[i].length <= 10
49+
* • groups[i] is either 0 or 1.
50+
* • words consists of distinct strings.
51+
* • words[i] consists of lowercase English letters.
52+
*
53+
* Hint 1:
54+
* This problem can be solved greedily.
55+
*
56+
* Hint 2:
57+
* Begin by constructing the answer starting with the first number in groups.
58+
*
59+
* Hint 3:
60+
* For each index i in the range [1, n - 1],
61+
* add i to the answer if groups[i] != groups[i - 1].
62+
**
63+
* https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i/
64+
***/
65+
66+
using System.Collections.Generic;
67+
68+
namespace Problems;
69+
70+
public class LongestUnequalAdjacentGroupsSubsequenceI
71+
{
72+
public IList<string> GetLongestSubsequence( string[] words, int[] groups )
73+
{
74+
List<string> result = new( words.Length ) { words[0] };
75+
76+
for ( int right = 1; right < groups.Length; right++ )
77+
{
78+
if ( groups[right] != groups[right - 1] )
79+
{
80+
result.Add( words[right] );
81+
}
82+
}
83+
84+
return result; ;
85+
}
86+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
using NUnit.Framework;
4+
5+
using Problems;
6+
7+
public class LongestUnequalAdjacentGroupsSubsequenceITests
8+
{
9+
[TestCase( new string[] { "c" }, new int[] { 0 }, ExpectedResult = new string[] { "c" } )]
10+
[TestCase( new string[] { "d" }, new int[] { 1 }, ExpectedResult = new string[] { "d" } )]
11+
[TestCase( new string[] { "e", "a", "b" }, new int[] { 0, 0, 1 }, ExpectedResult = new string[] { "e", "b" } )]
12+
[TestCase( new string[] { "a", "b", "c", "d" }, new int[] { 1, 0, 1, 1 }, ExpectedResult = new string[] { "a", "b", "c" } )]
13+
[TestCase( new string[] { "d", "a", "v", "b" }, new int[] { 1, 0, 0, 1 }, ExpectedResult = new string[] { "d", "a", "b" } )]
14+
public IList<string> GetLongestSubsequenceTest( string[] words, int[] groups ) =>
15+
new LongestUnequalAdjacentGroupsSubsequenceI().GetLongestSubsequence( words, groups );
16+
}

0 commit comments

Comments
 (0)