Skip to content

Commit 2887cf4

Browse files
committed
Sync LeetCode submission Runtime - 2 ms (32.18%), Memory - 17.9 MB (22.15%)
1 parent d9fbc0c commit 2887cf4

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<p>You are given a string array <code>words</code> and a <strong>binary</strong> array <code>groups</code> both of length <code>n</code>, where <code>words[i]</code> is associated with <code>groups[i]</code>.</p>
2+
3+
<p>Your task is to select the <strong>longest alternating</strong> <span data-keyword="subsequence-array">subsequence</span> from <code>words</code>. A subsequence of <code>words</code> is alternating if for any two consecutive strings in the sequence, their corresponding elements in the binary array <code>groups</code> differ. Essentially, you are to choose strings such that adjacent elements have non-matching corresponding bits in the <code>groups</code> array.</p>
4+
5+
<p>Formally, you need to find the longest subsequence of an array of indices <code>[0, 1, ..., n - 1]</code> denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k-1</sub>]</code>, such that <code>groups[i<sub>j</sub>] != groups[i<sub>j+1</sub>]</code> for each <code>0 &lt;= j &lt; k - 1</code> and then find the words corresponding to these indices.</p>
6+
7+
<p>Return <em>the selected subsequence. If there are multiple answers, return <strong>any</strong> of them.</em></p>
8+
9+
<p><strong>Note:</strong> The elements in <code>words</code> are distinct.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<div class="example-block" style="
15+
border-color: var(--border-tertiary);
16+
border-left-width: 2px;
17+
color: var(--text-secondary);
18+
font-size: .875rem;
19+
margin-bottom: 1rem;
20+
margin-top: 1rem;
21+
overflow: visible;
22+
padding-left: 1rem;
23+
">
24+
<p><strong>Input:</strong> <span class="example-io" style="
25+
font-family: Menlo,sans-serif;
26+
font-size: 0.85rem;
27+
">words = [&quot;e&quot;,&quot;a&quot;,&quot;b&quot;], groups = [0,0,1]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io" style="
30+
font-family: Menlo,sans-serif;
31+
font-size: 0.85rem;
32+
">[&quot;e&quot;,&quot;b&quot;]</span></p>
33+
34+
<p><strong>Explanation:</strong> A subsequence that can be selected is <code>[&quot;e&quot;,&quot;b&quot;]</code> because <code>groups[0] != groups[2]</code>. Another subsequence that can be selected is <code>[&quot;a&quot;,&quot;b&quot;]</code> because <code>groups[1] != groups[2]</code>. It can be demonstrated that the length of the longest subsequence of indices that satisfies the condition is <code>2</code>.</p>
35+
</div>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<div class="example-block" style="
40+
border-color: var(--border-tertiary);
41+
border-left-width: 2px;
42+
color: var(--text-secondary);
43+
font-size: .875rem;
44+
margin-bottom: 1rem;
45+
margin-top: 1rem;
46+
overflow: visible;
47+
padding-left: 1rem;
48+
">
49+
<p><strong>Input:</strong> <span class="example-io" style="
50+
font-family: Menlo,sans-serif;
51+
font-size: 0.85rem;
52+
">words = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;], groups = [1,0,1,1]</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io" style="
55+
font-family: Menlo,sans-serif;
56+
font-size: 0.85rem;
57+
">[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</span></p>
58+
59+
<p><strong>Explanation:</strong> A subsequence that can be selected is <code>[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]</code> because <code>groups[0] != groups[1]</code> and <code>groups[1] != groups[2]</code>. Another subsequence that can be selected is <code>[&quot;a&quot;,&quot;b&quot;,&quot;d&quot;]</code> because <code>groups[0] != groups[1]</code> and <code>groups[1] != groups[3]</code>. It can be shown that the length of the longest subsequence of indices that satisfies the condition is <code>3</code>.</p>
60+
</div>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
67+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
68+
<li><code>groups[i]</code> is either <code>0</code> or <code>1.</code></li>
69+
<li><code>words</code> consists of <strong>distinct</strong> strings.</li>
70+
<li><code>words[i]</code> consists of lowercase English letters.</li>
71+
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Approach 2: Greedy
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
8+
return [words[0]] + [words[i] for i in range(1, len(groups)) if groups[i] != groups[i - 1]]

0 commit comments

Comments
 (0)