網頁

2025年3月8日 星期六

C++ Py3 Sliding window速解Leetcode 2379 Minimum Recolors to Get K Consec...


C++ Py3  Sliding window速解2379  Minimum Recolors to Get K Consecutive Black Blocks
[Py3 解含過程輸出請進]



class Solution:
    def minimumRecolors(self, blocks: str, k: int) -> int:
        n=len(blocks)
        recolor=W=blocks[:k].count('W')
        print(f"1st window recolor={recolor} W={W}")
        for l in range(n-k):
            W+=(blocks[l+k]=='W')-(blocks[l]=='W')
            recolor=min(recolor, W)
            print(f"remove l={l}  add r={l+k} recolor={recolor} W={W}")
        return recolor

沒有留言:

張貼留言

HTML 編輯器