Hyoseo Lee
01 Index 02 Current page Writing 03 Work 04 Notes 05 Games 06 Author
leetcode

2379. Minimum Recolors to Get K Consecutive Black Blocks

Topic Easy
Area Algorithms

Problem

View on LeetCode →

Difficulty: Easy
Tags: Mid Level, String, Sliding Window, Biweekly Contest 85

Intuition

This one was also too easy. this was basically the same problem with 2269. but with different words.

All i need to do is counting White blocks in consecutive k-element substring.

Approach

go through all k-substring of blocks, and find the minimum number of W block in that sub-string.

Solution

class Solution:
    def minimumRecolors(self, blocks: str, k: int) -> int:
        ans = k
        for i in range(len(blocks)-k+1):
            tmp = blocks[i:i+k].count("W")
            if tmp < ans:
                ans = tmp
        return ans

Complexity

  • Time: O(n)O(n)

  • Space: O(1)O(1)

Thoughts

This was too easy. maybe it’s time to level up to Medium level