Skip to content

Commit 194f6f7

Browse files
authored
Create alternating-groups-ii.py
1 parent 86c8bae commit 194f6f7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Python/alternating-groups-ii.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# sliding window, two pointers
5+
class Solution(object):
6+
def numberOfAlternatingGroups(self, colors, k):
7+
"""
8+
:type colors: List[int]
9+
:type k: int
10+
:rtype: int
11+
"""
12+
result = curr = left = 0
13+
for right in xrange(len(colors)+k-1):
14+
if right-left+1 == k:
15+
result += int(curr == k-1)
16+
curr -= int(colors[left] != colors[(left+1)%len(colors)])
17+
left += 1
18+
curr += int(colors[right%len(colors)] != colors[(right+1)%len(colors)])
19+
return result

0 commit comments

Comments
 (0)