We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f346697 commit 5c58ae3Copy full SHA for 5c58ae3
3201-3300/3208_alternating_groups_ii.rb
@@ -0,0 +1,19 @@
1
+# @param {Integer[]} colors
2
+# @param {Integer} k
3
+# @return {Integer}
4
+def number_of_alternating_groups(colors, k)
5
+ count = 0
6
+ alter_length = 0
7
+ size = colors.size
8
+ # concat 2 colors to deal with circular
9
+ (0...2*size).each { |i|
10
+ if colors[i % size] != colors[(i-1) % size]
11
+ alter_length += 1
12
+ # to avoid counting duplicate groups, only count from the second colors
13
+ count += 1 if alter_length >= k && i >= size
14
+ else
15
+ alter_length = 1
16
+ end
17
+ }
18
+ count
19
+end
0 commit comments