Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution

Problem – Minimum Recolors to Get K Consecutive Black Blocks

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: 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:

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.

Constraints:

  • n == blocks.length
  • 1 <= n <= 100
  • blocks[i] is either 'W' or 'B'.
  • 1 <= k <= n

Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution in C++

    int minimumRecolors(string blocks, int k) {
        int back = 0, front = 0, count_w = 0, ans = INT_MAX;
        while(front < blocks.size()){
            if(blocks[front] == 'W'){ count_w++; }
            if(front - back + 1 >= k){
                ans = min(ans, count_w);
                if(blocks[back] == 'W') count_w--;
                back++;
            }
            front++;
        }
        return ans;
    }

Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution in Java

    public int minimumRecolors(String blocks, int k) {
        int min = Integer.MAX_VALUE;
        for (int lo = -1, hi = 0, white = 0; hi < blocks.length(); ++hi) {
            white += blocks.charAt(hi) == 'W' ? 1 : 0;
            if (hi - lo >= k) { // the window reaches size of k.
                min = Math.min(min, white); // update  minimum.
                // slide 1 step right the lower bound of the sliding
                // window and update the value of white count.
                white -= blocks.charAt(++lo) == 'W' ? 1 : 0;
            }
        }
        return min;
    }

Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution in Python

class Solution:
    def minimumRecolors(self, blocks: str, k: int) -> int:
        n, minOps, flips, count, i = len(blocks), 1e9, 0, 0, 0
        for j in range (n):
            if blocks[j] == 'W':
                flips += 1
                count += 1
            elif blocks[j] == 'B':
                count += 1
            if count == k:
                minOps = min(minOps, flips)
                if blocks[i] == 'W':
                    flips -= 1
                    count -= 1
                else: count -= 1
                i += 1
        return minOps
Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution Review:

In our experience, we suggest you solve this Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.