Skip to content

Commit bcdc734

Browse files
committed
feat: add solutions to lc problems: No.2383~2386
* No.2383.Minimum Hours of Training to Win a Competition * No.2384.Largest Palindromic Number * No.2385.Amount of Time for Binary Tree to Be Infected * No.2386.Find the K-Sum of an Array
1 parent 7ba0acb commit bcdc734

File tree

41 files changed

+2895
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2895
-15
lines changed

solution/1400-1499/1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
将 $sentence$ 按空格分割为 $words$,然后遍历 $words$,检查 $words[i]$ 是否是 $searchWord$ 的前缀,是则返回 $i+1$。若遍历结束,所有单词都不满足,返回 $-1$。
5959

60-
时间复杂度 $O(mn)$。其中 $m$ 是 $sentence$ 的长度,$n$ 是 $searchWord$ 的长度。
60+
时间复杂度 $O(mn)$。其中 $m$ 是 $sentence$ 的长度,$n$ 是 $searchWord$ 的长度。
6161

6262

6363
<!-- tabs:start -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2379. 得到 K 个黑块的最少涂色次数](https://leetcode.cn/problems/minimum-recolors-to-get-k-consecutive-black-blocks)
2+
3+
[English Version](/solution/2300-2399/2379.Minimum%20Recolors%20to%20Get%20K%20Consecutive%20Black%20Blocks/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>blocks</code>&nbsp;,<code>blocks[i]</code>&nbsp;要么是&nbsp;<code>'W'</code>&nbsp;要么是&nbsp;<code>'B'</code>&nbsp;,表示第&nbsp;<code>i</code>&nbsp;块的颜色。字符&nbsp;<code>'W'</code> 和&nbsp;<code>'B'</code>&nbsp;分别表示白色和黑色。</p>
10+
11+
<p>给你一个整数&nbsp;<code>k</code>&nbsp;,表示想要&nbsp;<strong>连续</strong>&nbsp;黑色块的数目。</p>
12+
13+
<p>每一次操作中,你可以选择一个白色块将它 <strong>涂成</strong>&nbsp;黑色块。</p>
14+
15+
<p>请你返回至少出现 <strong>一次</strong>&nbsp;连续 <code>k</code>&nbsp;个黑色块的 <strong>最少</strong>&nbsp;操作次数。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<b>输入:</b>blocks = "WBBWWBBWBW", k = 7
23+
<b>输出:</b>3
24+
<strong>解释:</strong>
25+
一种得到 7 个连续黑色块的方法是把第 0 ,3 和 4 个块涂成黑色。
26+
得到 blocks = "BBBBBBBWBW" 。
27+
可以证明无法用少于 3 次操作得到 7 个连续的黑块。
28+
所以我们返回 3 。
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre>
34+
<b>输入:</b>blocks = "WBWBBBW", k = 2
35+
<b>输出:</b>0
36+
<strong>解释:</strong>
37+
不需要任何操作,因为已经有 2 个连续的黑块。
38+
所以我们返回 0 。
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><b>提示:</b></p>
44+
45+
<ul>
46+
<li><code>n == blocks.length</code></li>
47+
<li><code>1 &lt;= n &lt;= 100</code></li>
48+
<li><code>blocks[i]</code>&nbsp;要么是&nbsp;<code>'W'</code>&nbsp;,要么是&nbsp;<code>'B'</code> 。</li>
49+
<li><code>1 &lt;= k &lt;= n</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```java
71+
72+
```
73+
74+
### **TypeScript**
75+
76+
```ts
77+
78+
```
79+
80+
### **...**
81+
82+
```
83+
84+
85+
```
86+
87+
<!-- tabs:end -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [2379. Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks)
2+
3+
[中文文档](/solution/2300-2399/2379.Minimum%20Recolors%20to%20Get%20K%20Consecutive%20Black%20Blocks/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> string <code>blocks</code> of length <code>n</code>, where <code>blocks[i]</code> is either <code>&#39;W&#39;</code> or <code>&#39;B&#39;</code>, representing the color of the <code>i<sup>th</sup></code> block. The characters <code>&#39;W&#39;</code> and <code>&#39;B&#39;</code> denote the colors white and black, respectively.</p>
8+
9+
<p>You are also given an integer <code>k</code>, which is the desired number of <strong>consecutive</strong> black blocks.</p>
10+
11+
<p>In one operation, you can <strong>recolor</strong> a white block such that it becomes a black block.</p>
12+
13+
<p>Return<em> the <strong>minimum</strong> number of operations needed such that there is at least <strong>one</strong> occurrence of </em><code>k</code><em> consecutive black blocks.</em></p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> blocks = &quot;WBBWWBBWBW&quot;, k = 7
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong>
22+
One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks
23+
so that blocks = &quot;BBBBBBBWBW&quot;.
24+
It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations.
25+
Therefore, we return 3.
26+
</pre>
27+
28+
<p><strong>Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> blocks = &quot;WBWBBBW&quot;, k = 2
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong>
34+
No changes need to be made, since 2 consecutive black blocks already exist.
35+
Therefore, we return 0.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>n == blocks.length</code></li>
43+
<li><code>1 &lt;= n &lt;= 100</code></li>
44+
<li><code>blocks[i]</code> is either <code>&#39;W&#39;</code> or <code>&#39;B&#39;</code>.</li>
45+
<li><code>1 &lt;= k &lt;= n</code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
62+
```
63+
64+
### **TypeScript**
65+
66+
```ts
67+
68+
```
69+
70+
### **...**
71+
72+
```
73+
74+
75+
```
76+
77+
<!-- tabs:end -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2380. 二进制字符串重新安排顺序需要的时间](https://leetcode.cn/problems/time-needed-to-rearrange-a-binary-string)
2+
3+
[English Version](/solution/2300-2399/2380.Time%20Needed%20to%20Rearrange%20a%20Binary%20String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个二进制字符串&nbsp;<code>s</code>&nbsp;。在一秒之中,<strong>所有</strong>&nbsp;子字符串&nbsp;<code>"01"</code> <strong>同时</strong>&nbsp;被替换成&nbsp;<code>"10"</code>&nbsp;。这个过程持续进行到没有&nbsp;<code>"01"</code>&nbsp;存在。</p>
10+
11+
<p>请你返回完成这个过程所需要的秒数。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre>
18+
<b>输入:</b>s = "0110101"
19+
<b>输出:</b>4
20+
<b>解释:</b>
21+
一秒后,s 变成 "1011010" 。
22+
再过 1 秒后,s 变成 "1101100" 。
23+
第三秒过后,s 变成 "1110100" 。
24+
第四秒后,s 变成 "1111000" 。
25+
此时没有 "01" 存在,整个过程花费 4 秒。
26+
所以我们返回 4 。
27+
</pre>
28+
29+
<p><strong>示例 2:</strong></p>
30+
31+
<pre>
32+
<b>输入:</b>s = "11100"
33+
<b>输出:</b>0
34+
<strong>解释:</strong>
35+
s 中没有 "01" 存在,整个过程花费 0 秒。
36+
所以我们返回 0 。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
45+
<li><code>s[i]</code>&nbsp;要么是&nbsp;<code>'0'</code>&nbsp;,要么是&nbsp;<code>'1'</code> 。</li>
46+
</ul>
47+
48+
## 解法
49+
50+
<!-- 这里可写通用的实现逻辑 -->
51+
52+
<!-- tabs:start -->
53+
54+
### **Python3**
55+
56+
<!-- 这里可写当前语言的特殊实现逻辑 -->
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```java
67+
68+
```
69+
70+
### **TypeScript**
71+
72+
```ts
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
81+
```
82+
83+
<!-- tabs:end -->
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [2380. Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string)
2+
3+
[中文文档](/solution/2300-2399/2380.Time%20Needed%20to%20Rearrange%20a%20Binary%20String/README.md)
4+
5+
## Description
6+
7+
<p>You are given a binary string <code>s</code>. In one second, <strong>all</strong> occurrences of <code>&quot;01&quot;</code> are <strong>simultaneously</strong> replaced with <code>&quot;10&quot;</code>. This process <strong>repeats</strong> until no occurrences of <code>&quot;01&quot;</code> exist.</p>
8+
9+
<p>Return<em> the number of seconds needed to complete this process.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> s = &quot;0110101&quot;
16+
<strong>Output:</strong> 4
17+
<strong>Explanation:</strong>
18+
After one second, s becomes &quot;1011010&quot;.
19+
After another second, s becomes &quot;1101100&quot;.
20+
After the third second, s becomes &quot;1110100&quot;.
21+
After the fourth second, s becomes &quot;1111000&quot;.
22+
No occurrence of &quot;01&quot; exists any longer, and the process needed 4 seconds to complete,
23+
so we return 4.
24+
</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> s = &quot;11100&quot;
30+
<strong>Output:</strong> 0
31+
<strong>Explanation:</strong>
32+
No occurrence of &quot;01&quot; exists in s, and the processes needed 0 seconds to complete,
33+
so we return 0.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
41+
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
42+
</ul>
43+
44+
## Solutions
45+
46+
<!-- tabs:start -->
47+
48+
### **Python3**
49+
50+
```python
51+
52+
```
53+
54+
### **Java**
55+
56+
```java
57+
58+
```
59+
60+
### **TypeScript**
61+
62+
```ts
63+
64+
```
65+
66+
### **...**
67+
68+
```
69+
70+
71+
```
72+
73+
<!-- tabs:end -->

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy