Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4dd1e54

Browse files
committed
2379
1 parent f251ac9 commit 4dd1e54

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /