LeetCode solutions

2463. Minimum Recolors to Get K Consecutive Black Blocks

My accepted Python solution to LeetCode problem 2463, Minimum Recolors to Get K Consecutive Black Blocks, running in 2ms.

  • Difficulty: Easy
  • Python
  • Runtime 2ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 2ms, memory 17.5MB, accepted 2025-12-31.

python
class Solution:
    def minimumRecolors(self, blocks: str, k: int) -> int:
        # Sliding window approach
        # Count white blocks in first window
        white_count = blocks[:k].count('W')
        min_recolors = white_count
        
        for i in range(k, len(blocks)):
            # Add new element, remove old element
            if blocks[i] == 'W':
                white_count += 1
            if blocks[i - k] == 'W':
                white_count -= 1
            min_recolors = min(min_recolors, white_count)
        
        return min_recolors

Source