Skip to content
LC-2379 Easy LeetCode

2379. Minimum Recolors to Get K Consecutive Black Blocks

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 68% Topics: String, Sliding Window
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# sliding window
class Solution(object):
    def minimumRecolors(self, blocks, k):
        """
        :type blocks: str
        :type k: int
        :rtype: int
        """
        result = k
        curr = 0
        for i, x in enumerate(blocks):
            curr += int(blocks[i] == 'W')
            if i+1-k < 0:
                continue
            result = min(result, curr)
            curr -= int(blocks[i+1-k] == 'W')
        return result

Solution from kamyu104/LeetCode-Solutions · MIT