herrDeng網內搜尋

自訂搜尋

Ads

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 解含過程輸出請進]



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

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章