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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 2ms, memory 17.5MB, accepted 2025-12-31.
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