diff --git a/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/README.md b/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/README.md index 9c3ca6447..605dfe8f5 100755 --- a/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/README.md +++ b/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/README.md @@ -1,28 +1,35 @@ # [2379.Minimum Recolors to Get K Consecutive Black Blocks][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 +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. + +You are also given an integer `k`, which is the desired number of **consecutive** black blocks. + +In one operation, you can **recolor** a white block such that it becomes a black block. + +Return the **minimum** number of operations needed such that there is at least **one** occurrence of `k` consecutive black blocks. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: blocks = "WBBWWBBWBW", k = 7 +Output: 3 +Explanation: +One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks +so that blocks = "BBBBBBBWBW". +It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations. +Therefore, we return 3. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Recolors to Get K Consecutive Black Blocks -```go ``` - +Input: blocks = "WBWBBBW", k = 2 +Output: 0 +Explanation: +No changes need to be made, since 2 consecutive black blocks already exist. +Therefore, we return 0. +``` ## 结语 diff --git a/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution.go b/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution.go index d115ccf5e..9bd566b39 100644 --- a/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution.go +++ b/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(blocks string, k int) int { + w, b := 0, 0 + for i := 0; i < k; i++ { + if blocks[i] == 'W' { + w++ + continue + } + b++ + } + ans := w + start, end := 0, k + for ; end < len(blocks); end++ { + if blocks[end] == 'W' { + w++ + } else { + b++ + } + if blocks[start] == 'W' { + w-- + } else { + b-- + } + ans = min(ans, w) + start++ + } + return ans + } diff --git a/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution_test.go b/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution_test.go index 14ff50eb4..5469036f9 100644 --- a/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution_test.go +++ b/leetcode/2301-2400/2379.Minimum-Recolors-to-Get-K-Consecutive-Black-Blocks/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + blocks string + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "WBBWWBBWBW", 7, 3}, + {"TestCase2", "WBWBBBW", 2, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.blocks, 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.blocks, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }