diff options
author | Jack Sangdahl <[email protected]> | 2025-05-14 21:37:57 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-14 21:37:57 -0600 |
commit | 435488fe67056415982f091df0c4a4a87531e628 (patch) | |
tree | 1a14588b629789d80e222e9c4cb4961fe8ac3571 | |
parent | a4a3dcd62355e55120cb4a4d10d1c2d27c902dff (diff) | |
download | leetcode-435488fe67056415982f091df0c4a4a87531e628.tar.gz leetcode-435488fe67056415982f091df0c4a4a87531e628.zip |
2900. longest unequal adjacent groups subsequence 1
-rw-r--r-- | longest_unequal_adjacent_group1.c | 39 | ||||
-rw-r--r-- | longest_unequal_adjacent_group1.py | 33 |
2 files changed, 72 insertions, 0 deletions
diff --git a/longest_unequal_adjacent_group1.c b/longest_unequal_adjacent_group1.c new file mode 100644 index 0000000..ab8018a --- /dev/null +++ b/longest_unequal_adjacent_group1.c @@ -0,0 +1,39 @@ +// 2900. Longest Unequal Adjacent Groups Subsequence I +#include "leetcode.h" + +/* + * 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. return the selected + * subsequence. if there are multiple, return any of them. note: the elements in + * 'words' are distinct. + */ + +char **getLongestSubsequence(char **words, int wordsSize, int *groups, + int groupsSize, int *returnSize) { + int j = 1; + for (int i = 1; i < groupsSize; i++) + if (groups[i] != groups[i - 1]) + words[j++] = strdup(words[i]); + *returnSize = j; + return words; +} + +int main() { + char *w1[] = {"e", "a", "b"}, *w2[] = {"a", "b", "c", "d"}; + int g1[] = {0, 0, 1}, g2[] = {1, 0, 1, 1}, rs1, rs2; + char **gls1 = getLongestSubsequence((char **)w1, ARRAY_SIZE(w1), g1, + ARRAY_SIZE(g1), &rs1); + char **gls2 = getLongestSubsequence((char **)w2, ARRAY_SIZE(w2), g2, + ARRAY_SIZE(g2), &rs2); + for (int i = 0; i < rs1; i++) + printf("%s ", gls1[i]); // expect: e b + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%s ", gls2[i]); // expect: a b c + printf("\n"); +} diff --git a/longest_unequal_adjacent_group1.py b/longest_unequal_adjacent_group1.py new file mode 100644 index 0000000..629e9f9 --- /dev/null +++ b/longest_unequal_adjacent_group1.py @@ -0,0 +1,33 @@ +# 2900. Longest Unequal Adjacent Groups Subsequence I + +""" +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. return the selected +subsequence. if there are multiple, return any of them. note: the elements in +'words' are distinct. +""" + + +class Solution(object): + def getLongestSubsequence(self, words, groups): + """ + :type words: List[str] + :type groups: List[int] + :rtype: List[str] + """ + k = [0] + for i, j in enumerate(groups): + if j != groups[k[-1]]: + k.append(i) + return [words[i] for i in k] + + +if __name__ == "__main__": + obj = Solution() + print(obj.getLongestSubsequence(words=["e", "a", "b"], groups=[0, 0, 1])) + print(obj.getLongestSubsequence(words=["a", "b", "c", "d"], groups=[1, 0, 1, 1])) |