We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 86c8bae commit 194f6f7Copy full SHA for 194f6f7
Python/alternating-groups-ii.py
@@ -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