|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +CREATED AT: 2023年03月09日 |
| 4 | + |
| 5 | +URL: https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/ |
| 6 | + |
| 7 | +GITHUB: https://github.com/Jiezhi/myleetcode |
| 8 | + |
| 9 | +FileName: 2379-MinimumRecolorsToGetKConsecutiveBlackBlocks |
| 10 | + |
| 11 | +Difficulty: Easy |
| 12 | + |
| 13 | +Desc: |
| 14 | + |
| 15 | +Tag: |
| 16 | + |
| 17 | +See: |
| 18 | + |
| 19 | +""" |
| 20 | +from tool import * |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def minimumRecolors(self, blocks: str, k: int) -> int: |
| 24 | + cnt = sum(1 for c in blocks[:k] if c == 'B') |
| 25 | + ret = k - cnt |
| 26 | + for i in range(k, len(blocks)): |
| 27 | + if blocks[i - k] == 'B': |
| 28 | + cnt -= 1 |
| 29 | + if blocks[i] == 'B': |
| 30 | + cnt += 1 |
| 31 | + ret = min(ret, k - cnt) |
| 32 | + if ret == 0: |
| 33 | + return 0 |
| 34 | + return ret |
| 35 | + |
| 36 | + |
| 37 | +def test(): |
| 38 | + assert Solution().minimumRecolors(blocks = "WBBWWBBWBW", k = 7) == 3 |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + test() |
| 43 | + |
0 commit comments