Skip to content

Commit b6cbc56

Browse files
authored
Merge pull request #1145 from 0xff-dev/3208
Add solution and test-cases for problem 3208
2 parents b26d0cc + 9bae5d7 commit b6cbc56

File tree

11 files changed

+88
-25
lines changed

11 files changed

+88
-25
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

leetcode/3201-3300/3208.Alternating-Groups-II/README.md

+49-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
# [3208.Alternating Groups II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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]`:
5+
6+
- `colors[i] == 0` means that tile `i` is **red**.
7+
- `colors[i] == 1` means that tile `i` is **blue**.
8+
9+
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).
10+
11+
Return the number of **alternating** groups.
12+
13+
**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other.
14+
15+
**Example 1:**
16+
17+
![1](./1.png)
18+
19+
20+
![2](./2.png)
21+
22+
23+
![3](./3.png)
24+
25+
26+
![4](./4.png)
727

8-
**Example 1:**
928

1029
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
30+
Input: colors = [0,1,0,1,0], k = 3
31+
32+
Output: 3
1333
```
1434

15-
## 题意
16-
> ...
35+
**Example 2:**
36+
37+
![5](./5.png)
38+
39+
40+
![6](./6.png)
41+
42+
43+
![7](./7.png)
1744

18-
## 题解
1945

20-
### 思路1
21-
> ...
22-
Alternating Groups II
23-
```go
2446
```
47+
Input: colors = [0,1,0,0,1,0,1], k = 6
48+
2549
50+
Output: 2
51+
```
52+
53+
**Example 3:**
54+
55+
![8](./8.png)
56+
57+
```
58+
Input: colors = [1,1,0,1], k = 4
59+
60+
Output: 0
61+
```
2662

2763
## 结语
2864

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(colors []int, k int) int {
4+
l := len(colors)
5+
dp := make([]int, len(colors))
6+
dp[0] = 1
7+
for i := 1; i < l; i++ {
8+
if colors[i] != colors[i-1] {
9+
dp[i] = dp[i-1] + 1
10+
continue
11+
}
12+
dp[i] = 1
13+
}
14+
ans := 0
15+
for start := 0; start <= l-k; start++ {
16+
end := start + k - 1
17+
if dp[end] >= k {
18+
ans++
19+
}
20+
}
21+
if colors[0] != colors[l-1] {
22+
for start := l - k + 1; start < l; start++ {
23+
end := (start+k)%l - 1
24+
if dp[l-1] >= l-start && dp[end] >= end+1 {
25+
ans++
26+
}
27+
}
28+
}
29+
30+
return ans
531
}

leetcode/3201-3300/3208.Alternating-Groups-II/Solution_test.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{0, 1, 0, 1, 0}, 3, 3},
18+
{"TestCase2", []int{0, 1, 0, 0, 1, 0, 1}, 6, 2},
19+
{"TestCase3", []int{1, 1, 0, 1}, 4, 0},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)