Skip to content

Commit 1ddd687

Browse files
committed
feat: update solutions to lc problem: No.2379
No.2379.Minimum Recolors to Get K Consecutive Black Blocks
1 parent da7e70c commit 1ddd687

File tree

6 files changed

+45
-98
lines changed

6 files changed

+45
-98
lines changed

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

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@
5555

5656
**方法一:滑动窗口**
5757

58-
遍历 $blocks$,找出 $k$ 大小的窗口中的白色块个数的最小值
58+
我们观察发现,题目实际上求的是一个 $k$ 大小的滑动窗口中白色块的最小数量
5959

60-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 $blocks$ 的长度。
60+
因此,我们只需要遍历字符串 $blocks$,用一个变量 $cnt$ 统计当前窗口中白色块的数量,然后用一个变量 $ans$ 维护最小值即可。
61+
62+
遍历结束后即可得到答案。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $blocks$ 的长度。
6165

6266
<!-- tabs:start -->
6367

@@ -68,14 +72,11 @@
6872
```python
6973
class Solution:
7074
def minimumRecolors(self, blocks: str, k: int) -> int:
71-
cnt = blocks[:k].count('W')
72-
ans = cnt
73-
i, n = k, len(blocks)
74-
while i < n:
75+
ans = cnt = blocks[:k].count('W')
76+
for i in range(k, len(blocks)):
7577
cnt += blocks[i] == 'W'
7678
cnt -= blocks[i - k] == 'W'
7779
ans = min(ans, cnt)
78-
i += 1
7980
return ans
8081
```
8182

@@ -86,15 +87,12 @@ class Solution:
8687
```java
8788
class Solution {
8889
public int minimumRecolors(String blocks, int k) {
89-
int cnt = 0, n = blocks.length();
90-
int i = 0;
91-
for (; i < k; ++i) {
92-
if (blocks.charAt(i) == 'W') {
93-
++cnt;
94-
}
90+
int cnt = 0;
91+
for (int i = 0; i < k; ++i) {
92+
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
9593
}
9694
int ans = cnt;
97-
for (; i < n; ++i) {
95+
for (int i = k; i < blocks.length(); ++i) {
9896
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
9997
cnt -= blocks.charAt(i - k) == 'W' ? 1 : 0;
10098
ans = Math.min(ans, cnt);
@@ -110,11 +108,9 @@ class Solution {
110108
class Solution {
111109
public:
112110
int minimumRecolors(string blocks, int k) {
113-
int cnt = 0, n = blocks.size();
114-
int i = 0;
115-
for (; i < k; ++i) cnt += blocks[i] == 'W';
111+
int cnt = count(blocks.begin(), blocks.begin() + k, 'W');
116112
int ans = cnt;
117-
for (; i < n; ++i) {
113+
for (int i = k; i < blocks.size(); ++i) {
118114
cnt += blocks[i] == 'W';
119115
cnt -= blocks[i - k] == 'W';
120116
ans = min(ans, cnt);
@@ -128,32 +124,21 @@ public:
128124
129125
```go
130126
func minimumRecolors(blocks string, k int) int {
131-
cnt, n := 0, len(blocks)
132-
i := 0
133-
for ; i < k; i++ {
134-
if blocks[i] == 'W' {
135-
cnt++
136-
}
137-
}
127+
cnt := strings.Count(blocks[:k], "W")
138128
ans := cnt
139-
for ; i < n; i++ {
129+
for i := k; i < len(blocks); i++ {
140130
if blocks[i] == 'W' {
141131
cnt++
142132
}
143133
if blocks[i-k] == 'W' {
144134
cnt--
145135
}
146-
ans = min(ans, cnt)
136+
if ans > cnt {
137+
ans = cnt
138+
}
147139
}
148140
return ans
149141
}
150-
151-
func min(a, b int) int {
152-
if a < b {
153-
return a
154-
}
155-
return b
156-
}
157142
```
158143

159144
### **TypeScript**

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@ Therefore, we return 0.
5454
```python
5555
class Solution:
5656
def minimumRecolors(self, blocks: str, k: int) -> int:
57-
cnt = blocks[:k].count('W')
58-
ans = cnt
59-
i, n = k, len(blocks)
60-
while i < n:
57+
ans = cnt = blocks[:k].count('W')
58+
for i in range(k, len(blocks)):
6159
cnt += blocks[i] == 'W'
6260
cnt -= blocks[i - k] == 'W'
6361
ans = min(ans, cnt)
64-
i += 1
6562
return ans
6663
```
6764

@@ -70,15 +67,12 @@ class Solution:
7067
```java
7168
class Solution {
7269
public int minimumRecolors(String blocks, int k) {
73-
int cnt = 0, n = blocks.length();
74-
int i = 0;
75-
for (; i < k; ++i) {
76-
if (blocks.charAt(i) == 'W') {
77-
++cnt;
78-
}
70+
int cnt = 0;
71+
for (int i = 0; i < k; ++i) {
72+
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
7973
}
8074
int ans = cnt;
81-
for (; i < n; ++i) {
75+
for (int i = k; i < blocks.length(); ++i) {
8276
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
8377
cnt -= blocks.charAt(i - k) == 'W' ? 1 : 0;
8478
ans = Math.min(ans, cnt);
@@ -94,11 +88,9 @@ class Solution {
9488
class Solution {
9589
public:
9690
int minimumRecolors(string blocks, int k) {
97-
int cnt = 0, n = blocks.size();
98-
int i = 0;
99-
for (; i < k; ++i) cnt += blocks[i] == 'W';
91+
int cnt = count(blocks.begin(), blocks.begin() + k, 'W');
10092
int ans = cnt;
101-
for (; i < n; ++i) {
93+
for (int i = k; i < blocks.size(); ++i) {
10294
cnt += blocks[i] == 'W';
10395
cnt -= blocks[i - k] == 'W';
10496
ans = min(ans, cnt);
@@ -112,32 +104,21 @@ public:
112104
113105
```go
114106
func minimumRecolors(blocks string, k int) int {
115-
cnt, n := 0, len(blocks)
116-
i := 0
117-
for ; i < k; i++ {
118-
if blocks[i] == 'W' {
119-
cnt++
120-
}
121-
}
107+
cnt := strings.Count(blocks[:k], "W")
122108
ans := cnt
123-
for ; i < n; i++ {
109+
for i := k; i < len(blocks); i++ {
124110
if blocks[i] == 'W' {
125111
cnt++
126112
}
127113
if blocks[i-k] == 'W' {
128114
cnt--
129115
}
130-
ans = min(ans, cnt)
116+
if ans > cnt {
117+
ans = cnt
118+
}
131119
}
132120
return ans
133121
}
134-
135-
func min(a, b int) int {
136-
if a < b {
137-
return a
138-
}
139-
return b
140-
}
141122
```
142123

143124
### **TypeScript**

solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/Solution.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution {
22
public:
33
int minimumRecolors(string blocks, int k) {
4-
int cnt = 0, n = blocks.size();
5-
int i = 0;
6-
for (; i < k; ++i) cnt += blocks[i] == 'W';
4+
int cnt = count(blocks.begin(), blocks.begin() + k, 'W');
75
int ans = cnt;
8-
for (; i < n; ++i) {
6+
for (int i = k; i < blocks.size(); ++i) {
97
cnt += blocks[i] == 'W';
108
cnt -= blocks[i - k] == 'W';
119
ans = min(ans, cnt);
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
func minimumRecolors(blocks string, k int) int {
2-
cnt, n := 0, len(blocks)
3-
i := 0
4-
for ; i < k; i++ {
5-
if blocks[i] == 'W' {
6-
cnt++
7-
}
8-
}
2+
cnt := strings.Count(blocks[:k], "W")
93
ans := cnt
10-
for ; i < n; i++ {
4+
for i := k; i < len(blocks); i++ {
115
if blocks[i] == 'W' {
126
cnt++
137
}
148
if blocks[i-k] == 'W' {
159
cnt--
1610
}
17-
ans = min(ans, cnt)
11+
if ans > cnt {
12+
ans = cnt
13+
}
1814
}
1915
return ans
20-
}
21-
22-
func min(a, b int) int {
23-
if a < b {
24-
return a
25-
}
26-
return b
2716
}

solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/Solution.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution {
22
public int minimumRecolors(String blocks, int k) {
3-
int cnt = 0, n = blocks.length();
4-
int i = 0;
5-
for (; i < k; ++i) {
6-
if (blocks.charAt(i) == 'W') {
7-
++cnt;
8-
}
3+
int cnt = 0;
4+
for (int i = 0; i < k; ++i) {
5+
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
96
}
107
int ans = cnt;
11-
for (; i < n; ++i) {
8+
for (int i = k; i < blocks.length(); ++i) {
129
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
1310
cnt -= blocks.charAt(i - k) == 'W' ? 1 : 0;
1411
ans = Math.min(ans, cnt);
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
class Solution:
22
def minimumRecolors(self, blocks: str, k: int) -> int:
3-
cnt = blocks[:k].count('W')
4-
ans = cnt
5-
i, n = k, len(blocks)
6-
while i < n:
3+
ans = cnt = blocks[:k].count('W')
4+
for i in range(k, len(blocks)):
75
cnt += blocks[i] == 'W'
86
cnt -= blocks[i - k] == 'W'
97
ans = min(ans, cnt)
10-
i += 1
118
return ans

0 commit comments

Comments
 (0)