作者:
JIWP (JIWP)
2025-03-09 16:12:58好久沒發每日文了
最近的每日都偏簡單
3208. Alternating Groups II
這題就對阿
直接把前k-1個元素街道colors後面
接著就把colors[i] xor colors[i-1]
如果結果是1,就把cnt++
當cnt == k,答案就+1
如果結果是0,cnt重置為1
這樣就可以得到答案了
golang code
func numberOfAlternatingGroups(colors []int, k int) int {
colors = append(colors, colors[:k-1]...)
cnt, ans := 1, 0
for i := 1; i < len(colors); i++ {
if colors[i]^colors[i-1] == 1 {
cnt++
if cnt == k {
ans++
cnt