Skip to content

Commit b560720

Browse files
authored
Merge pull request #1144 from 0xff-dev/2379
Add solution and test-cases for problem 2379
2 parents b6cbc56 + ab240d3 commit b560720

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2379.Minimum Recolors to Get K Consecutive Black Blocks][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+
You are given a **0-indexed** string `blocks` of length `n`, where `blocks[i]` is either `'W'` or `'B'`, representing the color of the `ith` block. The characters `'W'` and `'B'` denote the colors white and black, respectively.
5+
6+
You are also given an integer `k`, which is the desired number of **consecutive** black blocks.
7+
8+
In one operation, you can **recolor** a white block such that it becomes a black block.
9+
10+
Return the **minimum** number of operations needed such that there is at least **one** occurrence of `k` consecutive black blocks.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: blocks = "WBBWWBBWBW", k = 7
16+
Output: 3
17+
Explanation:
18+
One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks
19+
so that blocks = "BBBBBBBWBW".
20+
It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations.
21+
Therefore, we return 3.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Recolors to Get K Consecutive Black Blocks
23-
```go
2426
```
25-
27+
Input: blocks = "WBWBBBW", k = 2
28+
Output: 0
29+
Explanation:
30+
No changes need to be made, since 2 consecutive black blocks already exist.
31+
Therefore, we return 0.
32+
```
2633

2734
## 结语
2835

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(blocks string, k int) int {
4+
w, b := 0, 0
5+
for i := 0; i < k; i++ {
6+
if blocks[i] == 'W' {
7+
w++
8+
continue
9+
}
10+
b++
11+
}
12+
ans := w
13+
start, end := 0, k
14+
for ; end < len(blocks); end++ {
15+
if blocks[end] == 'W' {
16+
w++
17+
} else {
18+
b++
19+
}
20+
if blocks[start] == 'W' {
21+
w--
22+
} else {
23+
b--
24+
}
25+
ans = min(ans, w)
26+
start++
27+
}
28+
return ans
29+
530
}

leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
blocks string
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "WBBWWBBWBW", 7, 3},
18+
{"TestCase2", "WBWBBBW", 2, 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.blocks, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.blocks, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)