-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3208_alternating_groups_ii.go
More file actions
47 lines (36 loc) · 1 KB
/
s3208_alternating_groups_ii.go
File metadata and controls
47 lines (36 loc) · 1 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
https://leetcode.com/problems/alternating-groups-ii/description/
There is a circle of red and blue tiles. You are given an array of integers
colors and an integer k.
The color of tile i is represented by colors[i]:
colors[i] == 0 means that tile i is red.
colors[i] == 1 means that tile i is blue.
An alternating group is every k contiguous tiles in the circle with alternating
colors (each tile in the group
except the first and last one has a different color from its left and right
tiles).
Return the number of alternating groups.
Note that since colors represents a circle, the first and the last tiles are
considered to be next to each other.
*/
package solutions
func numberOfAlternatingGroups(colors []int, k int) int {
res := 0
n := len(colors)
grLen := 1
lastC := colors[0]
for i := 0; i < n+k-1; i++ {
idx := i % n
if colors[idx] == lastC {
grLen = 1
lastC = colors[idx]
continue
}
grLen++
if grLen >= k {
res++
}
lastC = colors[idx]
}
return res
}