diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/1.png b/leetcode/3201-3300/3208.Alternating-Groups-II/1.png new file mode 100644 index 000000000..f87fb906d Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/1.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/2.png b/leetcode/3201-3300/3208.Alternating-Groups-II/2.png new file mode 100644 index 000000000..807d1a0aa Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/2.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/3.png b/leetcode/3201-3300/3208.Alternating-Groups-II/3.png new file mode 100644 index 000000000..842d33689 Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/3.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/4.png b/leetcode/3201-3300/3208.Alternating-Groups-II/4.png new file mode 100644 index 000000000..1559223e7 Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/4.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/5.png b/leetcode/3201-3300/3208.Alternating-Groups-II/5.png new file mode 100644 index 000000000..c4fde8821 Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/5.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/6.png b/leetcode/3201-3300/3208.Alternating-Groups-II/6.png new file mode 100644 index 000000000..e53fb9066 Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/6.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/7.png b/leetcode/3201-3300/3208.Alternating-Groups-II/7.png new file mode 100644 index 000000000..d516080a1 Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/7.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/8.png b/leetcode/3201-3300/3208.Alternating-Groups-II/8.png new file mode 100644 index 000000000..e9c963823 Binary files /dev/null and b/leetcode/3201-3300/3208.Alternating-Groups-II/8.png differ diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/README.md b/leetcode/3201-3300/3208.Alternating-Groups-II/README.md index 4d5d850a1..b297755c8 100755 --- a/leetcode/3201-3300/3208.Alternating-Groups-II/README.md +++ b/leetcode/3201-3300/3208.Alternating-Groups-II/README.md @@ -1,28 +1,64 @@ # [3208.Alternating Groups II][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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. + +**Example 1:** + +![1](./1.png) + + +![2](./2.png) + + +![3](./3.png) + + +![4](./4.png) -**Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: colors = [0,1,0,1,0], k = 3 + +Output: 3 ``` -## 题意 -> ... +**Example 2:** + +![5](./5.png) + + +![6](./6.png) + + +![7](./7.png) -## 题解 -### 思路1 -> ... -Alternating Groups II -```go ``` +Input: colors = [0,1,0,0,1,0,1], k = 6 + +Output: 2 +``` + +**Example 3:** + +![8](./8.png) + +``` +Input: colors = [1,1,0,1], k = 4 + +Output: 0 +``` ## 结语 diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/Solution.go b/leetcode/3201-3300/3208.Alternating-Groups-II/Solution.go index d115ccf5e..cf0418fe1 100644 --- a/leetcode/3201-3300/3208.Alternating-Groups-II/Solution.go +++ b/leetcode/3201-3300/3208.Alternating-Groups-II/Solution.go @@ -1,5 +1,31 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(colors []int, k int) int { + l := len(colors) + dp := make([]int, len(colors)) + dp[0] = 1 + for i := 1; i < l; i++ { + if colors[i] != colors[i-1] { + dp[i] = dp[i-1] + 1 + continue + } + dp[i] = 1 + } + ans := 0 + for start := 0; start <= l-k; start++ { + end := start + k - 1 + if dp[end] >= k { + ans++ + } + } + if colors[0] != colors[l-1] { + for start := l - k + 1; start < l; start++ { + end := (start+k)%l - 1 + if dp[l-1] >= l-start && dp[end] >= end+1 { + ans++ + } + } + } + + return ans } diff --git a/leetcode/3201-3300/3208.Alternating-Groups-II/Solution_test.go b/leetcode/3201-3300/3208.Alternating-Groups-II/Solution_test.go index 14ff50eb4..3a837d368 100644 --- a/leetcode/3201-3300/3208.Alternating-Groups-II/Solution_test.go +++ b/leetcode/3201-3300/3208.Alternating-Groups-II/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{0, 1, 0, 1, 0}, 3, 3}, + {"TestCase2", []int{0, 1, 0, 0, 1, 0, 1}, 6, 2}, + {"TestCase3", []int{1, 1, 0, 1}, 4, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }