Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 21ec9d5

Browse files
committed
feat: add solutions to lc problem: No.2379~2382
* No.2379.Minimum Recolors to Get K Consecutive Black Blocks * No.2380.Time Needed to Rearrange a Binary String * No.2381.Shifting Letters II * No.2382.Maximum Segment Sum After Removals
1 parent 2d57343 commit 21ec9d5

File tree

27 files changed

+1348
-9
lines changed

27 files changed

+1348
-9
lines changed

‎solution/0900-0999/0998.Maximum Binary Tree II/Solution.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
8+
def insertIntoMaxTree(
9+
self, root: Optional[TreeNode], val: int
10+
) -> Optional[TreeNode]:
911
if root is None or root.val < val:
1012
return TreeNode(val, root)
1113
root.right = self.insertIntoMaxTree(root.right, val)

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,107 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:滑动窗口**
57+
58+
遍历 $blocks,ドル找出 $k$ 大小的窗口中的白色块个数的最小值。
59+
60+
时间复杂都 $O(n),ドル空间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
<!-- 这里可写当前语言的特殊实现逻辑 -->
6167

6268
```python
63-
69+
class Solution:
70+
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+
cnt += blocks[i] == 'W'
76+
cnt -= blocks[i-k] == 'W'
77+
ans = min(ans, cnt)
78+
i += 1
79+
return ans
6480
```
6581

6682
### **Java**
6783

6884
<!-- 这里可写当前语言的特殊实现逻辑 -->
6985

7086
```java
87+
class Solution {
88+
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+
}
95+
}
96+
int ans = cnt;
97+
for (; i < n; ++i) {
98+
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
99+
cnt -= blocks.charAt(i - k) == 'W' ? 1 : 0;
100+
ans = Math.min(ans, cnt);
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
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';
116+
int ans = cnt;
117+
for (; i < n; ++i) {
118+
cnt += blocks[i] == 'W';
119+
cnt -= blocks[i - k] == 'W';
120+
ans = min(ans, cnt);
121+
}
122+
return ans;
123+
}
124+
};
125+
```
71126
127+
### **Go**
128+
129+
```go
130+
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+
}
138+
ans := cnt
139+
for ; i < n; i++ {
140+
if blocks[i] == 'W' {
141+
cnt++
142+
}
143+
if blocks[i-k] == 'W' {
144+
cnt--
145+
}
146+
ans = min(ans, cnt)
147+
}
148+
return ans
149+
}
150+
151+
func min(a, b int) int {
152+
if a < b {
153+
return a
154+
}
155+
return b
156+
}
72157
```
73158

74159
### **TypeScript**

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,92 @@ Therefore, we return 0.
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
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:
61+
cnt += blocks[i] == 'W'
62+
cnt -= blocks[i-k] == 'W'
63+
ans = min(ans, cnt)
64+
i += 1
65+
return ans
5666
```
5767

5868
### **Java**
5969

6070
```java
71+
class Solution {
72+
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+
}
79+
}
80+
int ans = cnt;
81+
for (; i < n; ++i) {
82+
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
83+
cnt -= blocks.charAt(i - k) == 'W' ? 1 : 0;
84+
ans = Math.min(ans, cnt);
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
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';
100+
int ans = cnt;
101+
for (; i < n; ++i) {
102+
cnt += blocks[i] == 'W';
103+
cnt -= blocks[i - k] == 'W';
104+
ans = min(ans, cnt);
105+
}
106+
return ans;
107+
}
108+
};
109+
```
61110
111+
### **Go**
112+
113+
```go
114+
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+
}
122+
ans := cnt
123+
for ; i < n; i++ {
124+
if blocks[i] == 'W' {
125+
cnt++
126+
}
127+
if blocks[i-k] == 'W' {
128+
cnt--
129+
}
130+
ans = min(ans, cnt)
131+
}
132+
return ans
133+
}
134+
135+
func min(a, b int) int {
136+
if a < b {
137+
return a
138+
}
139+
return b
140+
}
62141
```
63142

64143
### **TypeScript**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
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';
7+
int ans = cnt;
8+
for (; i < n; ++i) {
9+
cnt += blocks[i] == 'W';
10+
cnt -= blocks[i - k] == 'W';
11+
ans = min(ans, cnt);
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
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+
}
9+
ans := cnt
10+
for ; i < n; i++ {
11+
if blocks[i] == 'W' {
12+
cnt++
13+
}
14+
if blocks[i-k] == 'W' {
15+
cnt--
16+
}
17+
ans = min(ans, cnt)
18+
}
19+
return ans
20+
}
21+
22+
func min(a, b int) int {
23+
if a < b {
24+
return a
25+
}
26+
return b
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
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+
}
9+
}
10+
int ans = cnt;
11+
for (; i < n; ++i) {
12+
cnt += blocks.charAt(i) == 'W' ? 1 : 0;
13+
cnt -= blocks.charAt(i - k) == 'W' ? 1 : 0;
14+
ans = Math.min(ans, cnt);
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
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:
7+
cnt += blocks[i] == 'W'
8+
cnt -= blocks[i - k] == 'W'
9+
ans = min(ans, cnt)
10+
i += 1
11+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /