Skip to content

Commit b44a117

Browse files
committed
Sync LeetCode submission Runtime - 739 ms (40.53%), Memory - 22.7 MB (11.63%)
1 parent 478db26 commit b44a117

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

3483-alternating-groups-ii/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<p>There is a circle of red and blue tiles. You are given an array of integers <code>colors</code> and an integer <code>k</code>. The color of tile <code>i</code> is represented by <code>colors[i]</code>:</p>
2+
3+
<ul>
4+
<li><code>colors[i] == 0</code> means that tile <code>i</code> is <strong>red</strong>.</li>
5+
<li><code>colors[i] == 1</code> means that tile <code>i</code> is <strong>blue</strong>.</li>
6+
</ul>
7+
8+
<p>An <strong>alternating</strong> group is every <code>k</code> contiguous tiles in the circle with <strong>alternating</strong> colors (each tile in the group except the first and last one has a different color from its <strong>left</strong> and <strong>right</strong> tiles).</p>
9+
10+
<p>Return the number of <strong>alternating</strong> groups.</p>
11+
12+
<p><strong>Note</strong> that since <code>colors</code> represents a <strong>circle</strong>, the <strong>first</strong> and the <strong>last</strong> tiles are considered to be next to each other.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<div class="example-block">
18+
<p><strong>Input:</strong> <span class="example-io">colors = [0,1,0,1,0], k = 3</span></p>
19+
20+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
21+
22+
<p><strong>Explanation:</strong></p>
23+
24+
<p><strong><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></strong></p>
25+
26+
<p>Alternating groups:</p>
27+
28+
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182448.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182844.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-183057.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
29+
</div>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">colors = [0,1,0,0,1,0,1], k = 6</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
37+
38+
<p><strong>Explanation:</strong></p>
39+
40+
<p><strong><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></strong></p>
41+
42+
<p>Alternating groups:</p>
43+
44+
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184128.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184240.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
45+
</div>
46+
47+
<p><strong class="example">Example 3:</strong></p>
48+
49+
<div class="example-block">
50+
<p><strong>Input:</strong> <span class="example-io">colors = [1,1,0,1], k = 4</span></p>
51+
52+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
53+
54+
<p><strong>Explanation:</strong></p>
55+
56+
<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p>
57+
</div>
58+
59+
<p>&nbsp;</p>
60+
<p><strong>Constraints:</strong></p>
61+
62+
<ul>
63+
<li><code>3 &lt;= colors.length &lt;= 10<sup>5</sup></code></li>
64+
<li><code>0 &lt;= colors[i] &lt;= 1</code></li>
65+
<li><code>3 &lt;= k &lt;= colors.length</code></li>
66+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach 1: Expanding the Array & Sliding Window
2+
3+
# Time: O(n + k)
4+
# Space: O(k)
5+
6+
class Solution:
7+
def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
8+
# Expand the array for cirular sequences
9+
for i in range(k - 1):
10+
colors.append(colors[i])
11+
12+
length = len(colors)
13+
result = 0
14+
left, right = 0, 1
15+
16+
while right < length:
17+
# Check if the current color is the same as the last one
18+
if colors[right] == colors[right - 1]:
19+
20+
# Pattern breaks, reset window from the current position
21+
left = right
22+
right += 1
23+
continue
24+
25+
right += 1
26+
27+
if right - left < k:
28+
continue
29+
30+
result += 1
31+
left += 1
32+
33+
return result
34+
35+

0 commit comments

Comments
 (0)