aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-08 18:38:03 -0700
committerJack Sangdahl <[email protected]>2025-03-08 18:38:03 -0700
commit9a3c2c66ce7572fc2ed8d502fc1f75438e47cf7d (patch)
tree1502cfcce86df05944569c48940170b12677894b
parent3148809e3af8febf1f684933da6f71f7af5fd2e6 (diff)
downloadleetcode-9a3c2c66ce7572fc2ed8d502fc1f75438e47cf7d.tar.gz
leetcode-9a3c2c66ce7572fc2ed8d502fc1f75438e47cf7d.zip
3208. alternating groups 2
-rw-r--r--alternating_groups2.c31
-rw-r--r--alternating_groups2.py34
2 files changed, 65 insertions, 0 deletions
diff --git a/alternating_groups2.c b/alternating_groups2.c
new file mode 100644
index 0000000..1e5d42f
--- /dev/null
+++ b/alternating_groups2.c
@@ -0,0 +1,31 @@
+// 3208. alternating groups ii
+#include "leetcode.h"
+
+/*
+ * there is a circle of red and blue tiles. you are given an array of integers
+ * 'colours' and an integer 'k'. the colour of tile 'i' is represented by
+ * 'colours[i]': '0' means that tile 'i' is red, and '1' means that tile 'i' is
+ * blue. an alternating group is every 'k' contiguous tiles in the circle with
+ * alternating colours. return the number of alternating groups. note that since
+ * 'colours' represents a circle, the first and last tiles are considered to be
+ * next to eachother.
+ */
+
+int numberOfAlternatingGroups(int *colours, int coloursSize, int k) {
+ int cnt = 0, left = 0;
+ for (int right = 0; right < coloursSize + k - 1; ++right) {
+ if (right &&
+ colours[right % coloursSize] == colours[(right - 1) % coloursSize])
+ left = right;
+ if (right - left + 1 >= k)
+ cnt++;
+ }
+ return cnt;
+}
+
+int main() {
+ int c1[] = {0, 1, 0, 1, 0}, c2[] = {0, 1, 0, 0, 1, 0, 1}, c3[] = {1, 1, 0, 1};
+ printf("%d\n", numberOfAlternatingGroups(c1, ARRAY_SIZE(c1), 3)); // expect: 3
+ printf("%d\n", numberOfAlternatingGroups(c2, ARRAY_SIZE(c2), 6)); // expect: 2
+ printf("%d\n", numberOfAlternatingGroups(c3, ARRAY_SIZE(c3), 4)); // expect: 0
+}
diff --git a/alternating_groups2.py b/alternating_groups2.py
new file mode 100644
index 0000000..733212f
--- /dev/null
+++ b/alternating_groups2.py
@@ -0,0 +1,34 @@
+# 3208. alternating groups ii
+
+"""
+there is a circle of red and blue tiles. you are given an array of integers
+'colours' and an integer 'k'. the colour of tile 'i' is represented by
+'colours[i]': '0' means that tile 'i' is red, and '1' means that tile 'i' is
+blue. an alternating group is every 'k' contiguous tiles in the circle with
+alternating colours. return the number of alternating groups. note that since
+'colours' represents a circle, the first and last tiles are considered to be
+next to eachother.
+"""
+
+
+class Solution(object):
+ def numberOfAlternatingGroups(self, colours, k):
+ """
+ :type colours: List[int]
+ :type k: int
+ :rtype: int
+ """
+ n, cnt, left = len(colours), 0, 0
+ for right in range(n + k - 1):
+ if right > 0 and colours[right % n] == colours[(right - 1) % n]:
+ left = right
+ if right - left + 1 >= k:
+ cnt += 1
+ return cnt
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numberOfAlternatingGroups(colours=[0, 1, 0, 1, 0], k=3))
+ print(obj.numberOfAlternatingGroups(colours=[0, 1, 0, 0, 1, 0, 1], k=6))
+ print(obj.numberOfAlternatingGroups(colours=[1, 1, 0, 1], k=4))