Skip to content

Add solution and test-cases for problem 3208 #1145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 49 additions & 13 deletions leetcode/3201-3300/3208.Alternating-Groups-II/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
30 changes: 28 additions & 2 deletions leetcode/3201-3300/3208.Alternating-Groups-II/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 11 additions & 10 deletions leetcode/3201-3300/3208.Alternating-Groups-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading