Skip to content

Commit eb965c5

Browse files
committed
feat: add solutions to lc problems: No.2108~2111
* No.2108.Find First Palindromic String in the Array * No.2109.Adding Spaces to a String * No.2110.Number of Smooth Descent Periods of a Stock * No.2111.Minimum Operations to Make the Array K-Increasing
1 parent d8aecca commit eb965c5

File tree

34 files changed

+2230
-24
lines changed

34 files changed

+2230
-24
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [1375. 二进制字符串前缀一致的次数](https://leetcode-cn.com/problems/number-of-times-binary-string-is-prefix-aligned)
2+
3+
[English Version](/solution/1300-1399/1375.Number%20of%20Times%20Binary%20String%20Is%20Prefix-Aligned/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为 <code>n</code> 、下标从 <strong>1</strong> 开始的二进制字符串,所有位最开始都是 <code>0</code> 。我们会按步翻转该二进制字符串的所有位(即,将 <code>0</code> 变为 <code>1</code>)。</p>
10+
11+
<p>给你一个下标从 <strong>1</strong> 开始的整数数组 <code>flips</code> ,其中 <code>flips[i]</code> 表示对应下标 <code>i</code> 的位将会在第 <code>i</code> 步翻转。</p>
12+
13+
<p>二进制字符串 <strong>前缀一致</strong> 需满足:在第 <code>i</code> 步之后,在 <strong>闭</strong> 区间&nbsp;<code>[1, i]</code> 内的所有位都是 1 ,而其他位都是 0 。</p>
14+
15+
<p>返回二进制字符串在翻转过程中 <strong>前缀一致</strong> 的次数。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>flips = [3,2,4,1,5]
23+
<strong>输出:</strong>2
24+
<strong>解释:</strong>二进制字符串最开始是 "00000" 。
25+
执行第 1 步:字符串变为 "00100" ,不属于前缀一致的情况。
26+
执行第 2 步:字符串变为 "01100" ,不属于前缀一致的情况。
27+
执行第 3 步:字符串变为 "01110" ,不属于前缀一致的情况。
28+
执行第 4 步:字符串变为 "11110" ,属于前缀一致的情况。
29+
执行第 5 步:字符串变为 "11111" ,属于前缀一致的情况。
30+
在翻转过程中,前缀一致的次数为 2 ,所以返回 2 。
31+
</pre>
32+
33+
<p><strong>示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>flips = [4,1,2,3]
37+
<strong>输出:</strong>1
38+
<strong>解释:</strong>二进制字符串最开始是 "0000" 。
39+
执行第 1 步:字符串变为 "0001" ,不属于前缀一致的情况。
40+
执行第 2 步:字符串变为 "1001" ,不属于前缀一致的情况。
41+
执行第 3 步:字符串变为 "1101" ,不属于前缀一致的情况。
42+
执行第 4 步:字符串变为 "1111" ,属于前缀一致的情况。
43+
在翻转过程中,前缀一致的次数为 1 ,所以返回 1 。</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>n == flips.length</code></li>
51+
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
52+
<li><code>flips</code> 是范围 <code>[1, n]</code> 中所有整数构成的一个排列</li>
53+
</ul>
54+
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```python
67+
68+
```
69+
70+
### **Java**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```java
75+
76+
```
77+
78+
### **TypeScript**
79+
80+
<!-- 这里可写当前语言的特殊实现逻辑 -->
81+
82+
```ts
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [1375. Number of Times Binary String Is Prefix-Aligned](https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned)
2+
3+
[中文文档](/solution/1300-1399/1375.Number%20of%20Times%20Binary%20String%20Is%20Prefix-Aligned/README.md)
4+
5+
## Description
6+
7+
<p>You have a <strong>1-indexed</strong> binary string of length <code>n</code> where all the bits are <code>0</code> initially. We will flip all the bits of this binary string (i.e., change them from <code>0</code> to <code>1</code>) one by one. You are given a <strong>1-indexed</strong> integer array <code>flips</code> where <code>flips[i]</code> indicates that the bit at index <code>i</code> will be flipped in the <code>i<sup>th</sup></code> step.</p>
8+
9+
<p>A binary string is <strong>prefix-aligned</strong> if, after the <code>i<sup>th</sup></code> step, all the bits in the <strong>inclusive</strong> range <code>[1, i]</code> are ones and all the other bits are zeros.</p>
10+
11+
<p>Return <em>the number of times the binary string is <strong>prefix-aligned</strong> during the flipping process</em>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> flips = [3,2,4,1,5]
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong> The binary string is initially &quot;00000&quot;.
20+
After applying step 1: The string becomes &quot;00100&quot;, which is not prefix-aligned.
21+
After applying step 2: The string becomes &quot;01100&quot;, which is not prefix-aligned.
22+
After applying step 3: The string becomes &quot;01110&quot;, which is not prefix-aligned.
23+
After applying step 4: The string becomes &quot;11110&quot;, which is prefix-aligned.
24+
After applying step 5: The string becomes &quot;11111&quot;, which is prefix-aligned.
25+
We can see that the string was prefix-aligned 2 times, so we return 2.
26+
</pre>
27+
28+
<p><strong>Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> flips = [4,1,2,3]
32+
<strong>Output:</strong> 1
33+
<strong>Explanation:</strong> The binary string is initially &quot;0000&quot;.
34+
After applying step 1: The string becomes &quot;0001&quot;, which is not prefix-aligned.
35+
After applying step 2: The string becomes &quot;1001&quot;, which is not prefix-aligned.
36+
After applying step 3: The string becomes &quot;1101&quot;, which is not prefix-aligned.
37+
After applying step 4: The string becomes &quot;1111&quot;, which is prefix-aligned.
38+
We can see that the string was prefix-aligned 1 time, so we return 1.
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>n == flips.length</code></li>
46+
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
47+
<li><code>flips</code> is a permutation of the integers in the range <code>[1, n]</code>.</li>
48+
</ul>
49+
50+
51+
## Solutions
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
```python
58+
59+
```
60+
61+
### **Java**
62+
63+
```java
64+
65+
```
66+
67+
### **TypeScript**
68+
69+
```ts
70+
71+
```
72+
73+
### **...**
74+
75+
```
76+
77+
```
78+
79+
<!-- tabs:end -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [1578. 使绳子变成彩色的最短时间](https://leetcode-cn.com/problems/minimum-time-to-make-rope-colorful)
2+
3+
[English Version](/solution/1500-1599/1578.Minimum%20Time%20to%20Make%20Rope%20Colorful/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Alice 把 <code>n</code> 个气球排列在一根绳子上。给你一个下标从 <strong>0</strong> 开始的字符串 <code>colors</code> ,其中 <code>colors[i]</code> 是第 <code>i</code> 个气球的颜色。</p>
10+
11+
<p>Alice 想要把绳子装扮成 <strong>彩色</strong> ,且她不希望两个连续的气球涂着相同的颜色,所以她喊来 Bob 帮忙。Bob 可以从绳子上移除一些气球使绳子变成 <strong>彩色</strong> 。给你一个下标从 <strong>0</strong> 开始的整数数组 <code>neededTime</code> ,其中 <code>neededTime[i]</code> 是 Bob 从绳子上移除第 <code>i</code> 个气球需要的时间(以秒为单位)。</p>
12+
13+
<p>返回 Bob 使绳子变成 <strong>彩色</strong> 需要的 <strong>最少时间</strong> 。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg" style="width: 404px; height: 243px;" />
19+
<pre>
20+
<strong>输入:</strong>colors = "abaac", neededTime = [1,2,3,4,5]
21+
<strong>输出:</strong>3
22+
<strong>解释:</strong>在上图中,'a' 是蓝色,'b' 是红色且 'c' 是绿色。
23+
Bob 可以移除下标 2 的蓝色气球。这将花费 3 秒。
24+
移除后,不存在两个连续的气球涂着相同的颜色。总时间 = 3 。</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg" style="width: 244px; height: 243px;" />
28+
<pre>
29+
<strong>输入:</strong>colors = "abc", neededTime = [1,2,3]
30+
<strong>输出:</strong>0
31+
<strong>解释:</strong>绳子已经是彩色的,Bob 不需要从绳子上移除任何气球。
32+
</pre>
33+
34+
<p><strong>示例 3:</strong></p>
35+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg" style="width: 404px; height: 243px;" />
36+
<pre>
37+
<strong>输入:</strong>colors = "aabaa", neededTime = [1,2,3,4,1]
38+
<strong>输出:</strong>2
39+
<strong>解释:</strong>Bob 会移除下标 0 和下标 4 处的气球。这两个气球各需要 1 秒来移除。
40+
移除后,不存在两个连续的气球涂着相同的颜色。总时间 = 1 + 1 = 2 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>n == colors.length == neededTime.length</code></li>
49+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
50+
<li><code>1 &lt;= neededTime[i] &lt;= 10<sup>4</sup></code></li>
51+
<li><code>colors</code> 仅由小写英文字母组成</li>
52+
</ul>
53+
54+
55+
## 解法
56+
57+
<!-- 这里可写通用的实现逻辑 -->
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```python
66+
67+
```
68+
69+
### **Java**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```java
74+
75+
```
76+
77+
### **TypeScript**
78+
79+
<!-- 这里可写当前语言的特殊实现逻辑 -->
80+
81+
```ts
82+
83+
```
84+
85+
### **...**
86+
87+
```
88+
89+
```
90+
91+
<!-- tabs:end -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [1578. Minimum Time to Make Rope Colorful](https://leetcode.com/problems/minimum-time-to-make-rope-colorful)
2+
3+
[中文文档](/solution/1500-1599/1578.Minimum%20Time%20to%20Make%20Rope%20Colorful/README.md)
4+
5+
## Description
6+
7+
<p>Alice has <code>n</code> balloons arranged on a rope. You are given a <strong>0-indexed</strong> string <code>colors</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> balloon.</p>
8+
9+
<p>Alice wants the rope to be <strong>colorful</strong>. She does not want <strong>two consecutive balloons</strong> to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it <strong>colorful</strong>. You are given a <strong>0-indexed</strong> integer array <code>neededTime</code> where <code>neededTime[i]</code> is the time (in seconds) that Bob needs to remove the <code>i<sup>th</sup></code> balloon from the rope.</p>
10+
11+
<p>Return <em>the <strong>minimum time</strong> Bob needs to make the rope <strong>colorful</strong></em>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg" style="width: 404px; height: 243px;" />
16+
<pre>
17+
<strong>Input:</strong> colors = &quot;abaac&quot;, neededTime = [1,2,3,4,5]
18+
<strong>Output:</strong> 3
19+
<strong>Explanation:</strong> In the above image, &#39;a&#39; is blue, &#39;b&#39; is red, and &#39;c&#39; is green.
20+
Bob can remove the blue balloon at index 2. This takes 3 seconds.
21+
There are no longer two consecutive balloons of the same color. Total time = 3.</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg" style="width: 244px; height: 243px;" />
25+
<pre>
26+
<strong>Input:</strong> colors = &quot;abc&quot;, neededTime = [1,2,3]
27+
<strong>Output:</strong> 0
28+
<strong>Explanation:</strong> The rope is already colorful. Bob does not need to remove any balloons from the rope.
29+
</pre>
30+
31+
<p><strong>Example 3:</strong></p>
32+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg" style="width: 404px; height: 243px;" />
33+
<pre>
34+
<strong>Input:</strong> colors = &quot;aabaa&quot;, neededTime = [1,2,3,4,1]
35+
<strong>Output:</strong> 2
36+
<strong>Explanation:</strong> Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
37+
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>n == colors.length == neededTime.length</code></li>
45+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
46+
<li><code>1 &lt;= neededTime[i] &lt;= 10<sup>4</sup></code></li>
47+
<li><code>colors</code> contains only lowercase English letters.</li>
48+
</ul>
49+
50+
51+
## Solutions
52+
53+
<!-- tabs:start -->
54+
55+
### **Python3**
56+
57+
```python
58+
59+
```
60+
61+
### **Java**
62+
63+
```java
64+
65+
```
66+
67+
### **TypeScript**
68+
69+
```ts
70+
71+
```
72+
73+
### **...**
74+
75+
```
76+
77+
```
78+
79+
<!-- tabs:end -->

0 commit comments

Comments
 (0)