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 Reading material
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