Skip to content

Commit ff84b8d

Browse files
committed
lc: add solution for minimum recolors to get k consecutive black blocks
1 parent 4f508f4 commit ff84b8d

File tree

1 file changed

+28
-0
lines changed
  • src/main/java/com/thealgorithms/leetcode/slidingwindow/minimumrecolorstogetkconsecutiveblackblocks

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.leetcode.slidingwindow.minimumrecolorstogetkconsecutiveblackblocks;
2+
3+
public class Solution {
4+
public int minimumRecolors(String blocks, int k) {
5+
int left = 0;
6+
int right = 0;
7+
int whiteCount = 0;
8+
int minRecolors = Integer.MAX_VALUE;
9+
10+
while (right < blocks.length()) {
11+
if (blocks.charAt(right) == 'W') {
12+
whiteCount++;
13+
}
14+
15+
if (right - left + 1 == k) {
16+
minRecolors = Math.min(minRecolors, whiteCount);
17+
if (blocks.charAt(left) == 'W') {
18+
whiteCount--;
19+
}
20+
left++;
21+
}
22+
right++;
23+
}
24+
return minRecolors;
25+
}
26+
}
27+
28+
// https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/description/

0 commit comments

Comments
 (0)