C++ Py3  Sliding window速解2379  Minimum Recolors to Get K Consecutive Black Blocks
[Py3 解含過程輸出請進]
[codes on Leetcode]https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/solutions/6510851/sliding-window-with-fixed-len-beats-100/
[Sliding window/2 pointer play list]https://www.youtube.com/watch?v=pi1XpQU_Yxo&list=PLYRlUBnWnd5KrCfs7qCFb-Bvn7dvnakFy
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
 
   
 
 
沒有留言:
張貼留言