3208. 交替组 II
为保证权益,题目请参考 3208. 交替组 II(From LeetCode).
解决方案1
Python
python
from typing import List
class Solution:
def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
n = len(colors)
cur_len = 1
for i in range(1, k):
if colors[i] ^ colors[i - 1]:
cur_len += 1
else:
cur_len = 1
ans: int = 0
for i in range(k, k + n):
a = i % n
b = (i - 1) % n
if colors[a] ^ colors[b]:
cur_len += 1
else:
cur_len = 1
if cur_len >= k:
ans += 1
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27