Skip to content

Commit 4dbdea4

Browse files
authored
feat: add solutions to lc problem: No.2379 (doocs#921)
No.2379.Minimum Recolors to Get K Consecutive Black Blocks
1 parent b56e2ac commit 4dbdea4

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,19 @@ func minimumRecolors(blocks string, k int) int {
144144
### **TypeScript**
145145

146146
```ts
147-
147+
function minimumRecolors(blocks: string, k: number): number {
148+
let cnt = 0;
149+
for (let i = 0; i < k; ++i) {
150+
cnt += blocks[i] === 'W' ? 1 : 0;
151+
}
152+
let ans = cnt;
153+
for (let i = k; i < blocks.length; ++i) {
154+
cnt += blocks[i] === 'W' ? 1 : 0;
155+
cnt -= blocks[i - k] === 'W' ? 1 : 0;
156+
ans = Math.min(ans, cnt);
157+
}
158+
return ans;
159+
}
148160
```
149161

150162
### **...**

solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README_EN.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,19 @@ func minimumRecolors(blocks string, k int) int {
124124
### **TypeScript**
125125

126126
```ts
127-
127+
function minimumRecolors(blocks: string, k: number): number {
128+
let cnt = 0;
129+
for (let i = 0; i < k; ++i) {
130+
cnt += blocks[i] === 'W' ? 1 : 0;
131+
}
132+
let ans = cnt;
133+
for (let i = k; i < blocks.length; ++i) {
134+
cnt += blocks[i] === 'W' ? 1 : 0;
135+
cnt -= blocks[i - k] === 'W' ? 1 : 0;
136+
ans = Math.min(ans, cnt);
137+
}
138+
return ans;
139+
}
128140
```
129141

130142
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minimumRecolors(blocks: string, k: number): number {
2+
let cnt = 0;
3+
for (let i = 0; i < k; ++i) {
4+
cnt += blocks[i] === 'W' ? 1 : 0;
5+
}
6+
let ans = cnt;
7+
for (let i = k; i < blocks.length; ++i) {
8+
cnt += blocks[i] === 'W' ? 1 : 0;
9+
cnt -= blocks[i - k] === 'W' ? 1 : 0;
10+
ans = Math.min(ans, cnt);
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)