Skip to content

Commit 8d0f211

Browse files
committed
feat: add solutions to lc problem: No.2379
No.2379.Minimum Recolors to Get K Consecutive Black Blocks
1 parent 4330e6b commit 8d0f211

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,56 @@ function minimumRecolors(blocks: string, k: number): number {
159159
}
160160
```
161161

162+
### **Rust**
163+
164+
```rust
165+
impl Solution {
166+
pub fn minimum_recolors(blocks: String, k: i32) -> i32 {
167+
let k = k as usize;
168+
let s = blocks.as_bytes();
169+
let n = s.len();
170+
let mut count = 0;
171+
for i in 0..k {
172+
if s[i] == b'B' {
173+
count += 1;
174+
}
175+
}
176+
let mut ans = k - count;
177+
for i in k..n {
178+
if s[i - k] == b'B' {
179+
count -= 1;
180+
}
181+
if s[i] == b'B' {
182+
count += 1;
183+
}
184+
ans = ans.min(k - count);
185+
}
186+
ans as i32
187+
}
188+
}
189+
```
190+
191+
### **C**
192+
193+
```c
194+
#define min(a, b) (((a) < (b)) ? (a) : (b))
195+
196+
int minimumRecolors(char *blocks, int k) {
197+
int n = strlen(blocks);
198+
int count = 0;
199+
for (int i = 0; i < k; i++) {
200+
count += blocks[i] == 'B';
201+
}
202+
int ans = k - count;
203+
for (int i = k; i < n; i++) {
204+
count -= blocks[i - k] == 'B';
205+
count += blocks[i] == 'B';
206+
ans = min(ans, k - count);
207+
}
208+
return ans;
209+
}
210+
```
211+
162212
### **...**
163213
164214
```

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,56 @@ function minimumRecolors(blocks: string, k: number): number {
139139
}
140140
```
141141

142+
### **Rust**
143+
144+
```rust
145+
impl Solution {
146+
pub fn minimum_recolors(blocks: String, k: i32) -> i32 {
147+
let k = k as usize;
148+
let s = blocks.as_bytes();
149+
let n = s.len();
150+
let mut count = 0;
151+
for i in 0..k {
152+
if s[i] == b'B' {
153+
count += 1;
154+
}
155+
}
156+
let mut ans = k - count;
157+
for i in k..n {
158+
if s[i - k] == b'B' {
159+
count -= 1;
160+
}
161+
if s[i] == b'B' {
162+
count += 1;
163+
}
164+
ans = ans.min(k - count);
165+
}
166+
ans as i32
167+
}
168+
}
169+
```
170+
171+
### **C**
172+
173+
```c
174+
#define min(a, b) (((a) < (b)) ? (a) : (b))
175+
176+
int minimumRecolors(char *blocks, int k) {
177+
int n = strlen(blocks);
178+
int count = 0;
179+
for (int i = 0; i < k; i++) {
180+
count += blocks[i] == 'B';
181+
}
182+
int ans = k - count;
183+
for (int i = k; i < n; i++) {
184+
count -= blocks[i - k] == 'B';
185+
count += blocks[i] == 'B';
186+
ans = min(ans, k - count);
187+
}
188+
return ans;
189+
}
190+
```
191+
142192
### **...**
143193
144194
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define min(a, b) (((a) < (b)) ? (a) : (b))
2+
3+
int minimumRecolors(char *blocks, int k) {
4+
int n = strlen(blocks);
5+
int count = 0;
6+
for (int i = 0; i < k; i++) {
7+
count += blocks[i] == 'B';
8+
}
9+
int ans = k - count;
10+
for (int i = k; i < n; i++) {
11+
count -= blocks[i - k] == 'B';
12+
count += blocks[i] == 'B';
13+
ans = min(ans, k - count);
14+
}
15+
return ans;
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn minimum_recolors(blocks: String, k: i32) -> i32 {
3+
let k = k as usize;
4+
let s = blocks.as_bytes();
5+
let n = s.len();
6+
let mut count = 0;
7+
for i in 0..k {
8+
if s[i] == b'B' {
9+
count += 1;
10+
}
11+
}
12+
let mut ans = k - count;
13+
for i in k..n {
14+
if s[i - k] == b'B' {
15+
count -= 1;
16+
}
17+
if s[i] == b'B' {
18+
count += 1;
19+
}
20+
ans = ans.min(k - count);
21+
}
22+
ans as i32
23+
}
24+
}

0 commit comments

Comments
 (0)