Skip to content

Commit 8f3f7c5

Browse files
committedMar 8, 2025
Add solution #2379
1 parent d8d5c14 commit 8f3f7c5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@
735735
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
736736
2364|[Count Number of Bad Pairs](./2364-count-number-of-bad-pairs.js)|Medium|
737737
2375|[Construct Smallest Number From DI String](./2375-construct-smallest-number-from-di-string.js)|Medium|
738+
2379|[Minimum Recolors to Get K Consecutive Black Blocks](./2379-minimum-recolors-to-get-k-consecutive-black-blocks.js)|Easy|
738739
2381|[Shifting Letters II](./2381-shifting-letters-ii.js)|Medium|
739740
2390|[Removing Stars From a String](./2390-removing-stars-from-a-string.js)|Medium|
740741
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2379. Minimum Recolors to Get K Consecutive Black Blocks
3+
* https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/
4+
* Difficulty: Easy
5+
*
6+
* You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B',
7+
* representing the color of the ith block. The characters 'W' and 'B' denote the colors white
8+
* and black, respectively.
9+
*
10+
* You are also given an integer k, which is the desired number of consecutive black blocks.
11+
*
12+
* In one operation, you can recolor a white block such that it becomes a black block.
13+
*
14+
* Return the minimum number of operations needed such that there is at least one occurrence
15+
* of k consecutive black blocks.
16+
*/
17+
18+
/**
19+
* @param {string} blocks
20+
* @param {number} k
21+
* @return {number}
22+
*/
23+
var minimumRecolors = function(blocks, k) {
24+
let count = 0;
25+
26+
for (let i = 0; i < k; i++) {
27+
if (blocks[i] === 'W') count++;
28+
}
29+
30+
let result = count;
31+
32+
for (let i = k; i < blocks.length; i++) {
33+
if (blocks[i] === 'W') count++;
34+
if (blocks[i - k] === 'W') count--;
35+
result = Math.min(result, count);
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)