Skip to content

Commit f52f261

Browse files
committed
lc: add solution for alternating groups II
1 parent d780968 commit f52f261

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

  • src/main/java/com/thealgorithms/leetcode/slidingwindow/alternatinggroupsII
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.thealgorithms.leetcode.slidingwindow.alternatinggroupsII;
2+
3+
public class Solution {
4+
public int numberOfAlternatingGroups(int[] colors, int k) {
5+
int n = colors.length, res = 0, count = 1;
6+
for (int i = 0; i < n + k - 2; i++) {
7+
count = colors[i % n] != colors[(i + 1) % n] ? count + 1 : 1;
8+
res += count >= k ? 1 : 0;
9+
}
10+
return res;
11+
}
12+
}
13+
14+
// https://leetcode.com/problems/alternating-groups-ii/?envType=daily-question&envId=2025-03-09

0 commit comments

Comments
 (0)