Skip to content

Commit 719301a

Browse files
committed
feat: add solutions to lc problems: No.2586~2589
* No.2586.Count the Number of Vowel Strings in Range * No.2587.Rearrange Array to Maximize Prefix Score * No.2588.Count the Number of Beautiful Subarrays * No.2589.Minimum Time to Complete All Tasks
1 parent e48dded commit 719301a

File tree

39 files changed

+1398
-26
lines changed

39 files changed

+1398
-26
lines changed

lcci/17.08.Circus Tower/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
<li><code>height.length == weight.length &lt;= 10000</code></li>
2020
</ul>
2121

22-
2322
## Solutions
2423

25-
2624
<!-- tabs:start -->
27-
### **Python3**
2825

26+
### **Python3**
2927

3028
```python
3129
class BinaryIndexedTree:

lcof2/剑指 Offer II 020. 回文子字符串的个数/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Solution:
7171
cnt += 1
7272
i, j = i - 1, j + 1
7373
return cnt
74-
74+
7575
n = len(s)
7676
return sum(f(i, i) + f(i, i + 1) for i in range(n))
7777
```

solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
<p>&nbsp;</p>
5050

51-
<p><strong>进阶:</strong>这道题与 <a href="https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/description/">寻找旋转排序数组中的最小值</a> 类似,但 <code>nums</code> 可能包含重复元素。允许重复会影响算法的时间复杂度吗?会如何影响,为什么?</p>
51+
<p><strong>进阶:</strong>这道题与 <a href="https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/">寻找旋转排序数组中的最小值</a> 类似,但 <code>nums</code> 可能包含重复元素。允许重复会影响算法的时间复杂度吗?会如何影响,为什么?</p>
5252

5353
## 解法
5454

solution/0100-0199/0168.Excel Sheet Column Title/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ C -> 3
1717
...
1818
Z -> 26
1919
AA -> 27
20-
AB -> 28
20+
AB -> 28
2121
...
2222
</pre>
2323

solution/0100-0199/0168.Excel Sheet Column Title/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ C -&gt; 3
1515
...
1616
Z -&gt; 26
1717
AA -&gt; 27
18-
AB -&gt; 28
18+
AB -&gt; 28
1919
...
2020
</pre>
2121

solution/0500-0599/0535.Encode and Decode TinyURL/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<strong>Explanation:</strong>
2929
Solution obj = new Solution();
3030
string tiny = obj.encode(url); // returns the encoded tiny url.
31-
string ans = obj.decode(tiny); // returns the original url after deconding it.
31+
string ans = obj.decode(tiny); // returns the original url after decoding it.
3232
</pre>
3333

3434
<p>&nbsp;</p>

solution/1500-1599/1516.Move Sub-Tree of N-Ary Tree/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Description
66

7-
<p>Given the <code>root</code> of an <a href="https://leetcode.com/explore/learn/card/n-ary-tree/">N-ary tree</a> of unique values, and two nodes of the tree <code>p</code> and <code>q</code>.</p>
7+
<p>Given the <code>root</code> of an <span data-keyword="keyword-slug">n-ary-tree</span> of unique values, and two nodes of the tree <code>p</code> and <code>q</code>.</p>
88

99
<p>You should move the subtree of the node <code>p</code> to become a direct child of node <code>q</code>. If <code>p</code> is already a direct child of <code>q</code>, do not change anything. Node <code>p</code> <strong>must be</strong> the last child in the children list of node <code>q</code>.</p>
1010

solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
- at t = 4, we move to the cell (1,1). It is possible because grid[1][1] &lt;= 4.
2727
- at t = 5, we move to the cell (1,2). It is possible because grid[1][2] &lt;= 5.
2828
- at t = 6, we move to the cell (1,3). It is possible because grid[1][3] &lt;= 6.
29-
- at t = 7, we move to the cell (2,3). It is possible because grid[1][3] &lt;= 7.
29+
- at t = 7, we move to the cell (2,3). It is possible because grid[2][3] &lt;= 7.
3030
The final time is 7. It can be shown that it is the minimum time possible.
3131
</pre>
3232

solution/2500-2599/2581.Count Number of Possible Root Nodes/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
6969
<li><code>edges</code>&nbsp;表示一棵有效的树。</li>
7070
<li><code>guesses[j]</code>&nbsp;是树中的一条边。</li>
71+
<li><code>guesses</code>&nbsp;是唯一的。</li>
7172
<li><code>0 &lt;= k &lt;= guesses.length</code></li>
7273
</ul>
7374

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# [2586. 统计范围内的元音字符串数](https://leetcode.cn/problems/count-the-number-of-vowel-strings-in-range)
2+
3+
[English Version](/solution/2500-2599/2586.Count%20the%20Number%20of%20Vowel%20Strings%20in%20Range/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 和两个整数:<code>left</code> 和 <code>right</code> 。</p>
10+
11+
<p>如果字符串以元音字母开头并以元音字母结尾,那么该字符串就是一个 <strong>元音字符串</strong> ,其中元音字母是 <code>'a'</code>、<code>'e'</code>、<code>'i'</code>、<code>'o'</code>、<code>'u'</code> 。</p>
12+
13+
<p>返回<em> </em><code>words[i]</code> 是元音字符串的数目,其中<em> </em><code>i</code> 在闭区间 <code>[left, right]</code> 内。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>words = ["are","amy","u"], left = 0, right = 2
21+
<strong>输出:</strong>2
22+
<strong>解释:</strong>
23+
- "are" 是一个元音字符串,因为它以 'a' 开头并以 'e' 结尾。
24+
- "amy" 不是元音字符串,因为它没有以元音字母结尾。
25+
- "u" 是一个元音字符串,因为它以 'u' 开头并以 'u' 结尾。
26+
在上述范围中的元音字符串数目为 2 。
27+
</pre>
28+
29+
<p><strong>示例 2:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
33+
<strong>输出:</strong>3
34+
<strong>解释:</strong>
35+
- "aeo" 是一个元音字符串,因为它以 'a' 开头并以 'o' 结尾。
36+
- "mu" 不是元音字符串,因为它没有以元音字母开头。
37+
- "ooo" 是一个元音字符串,因为它以 'o' 开头并以 'o' 结尾。
38+
- "artro" 是一个元音字符串,因为它以 'a' 开头并以 'o' 结尾。
39+
在上述范围中的元音字符串数目为 3 。
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
48+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
49+
<li><code>words[i]</code> 仅由小写英文字母组成</li>
50+
<li><code>0 &lt;= left &lt;= right &lt; words.length</code></li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
**方法一:模拟**
58+
59+
我们只需要遍历区间 `[left, right]` 内的字符串,判断其是否以元音字母开头和结尾即可。若是,则答案加一。
60+
61+
遍历结束后,返回答案即可。
62+
63+
时间复杂度 $O(m)$,空间复杂度 $O(1)$。其中 $m = right - left + 1$。
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
class Solution:
73+
def vowelStrings(self, words: List[str], left: int, right: int) -> int:
74+
return sum(w[0] in 'aeiou' and w[-1] in 'aeiou' for w in words[left: right + 1])
75+
```
76+
77+
### **Java**
78+
79+
<!-- 这里可写当前语言的特殊实现逻辑 -->
80+
81+
```java
82+
class Solution {
83+
public int vowelStrings(String[] words, int left, int right) {
84+
int ans = 0;
85+
for (int i = left; i <= right; ++i) {
86+
var w = words[i];
87+
if (check(w.charAt(0)) && check(w.charAt(w.length() - 1))) {
88+
++ans;
89+
}
90+
}
91+
return ans;
92+
}
93+
94+
private boolean check(char c) {
95+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int vowelStrings(vector<string>& words, int left, int right) {
106+
auto check = [](char c) -> bool {
107+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
108+
};
109+
int ans = 0;
110+
for (int i = left; i <= right; ++i) {
111+
auto w = words[i];
112+
ans += check(w[0]) && check(w[w.size() - 1]);
113+
}
114+
return ans;
115+
}
116+
};
117+
```
118+
119+
### **Go**
120+
121+
```go
122+
func vowelStrings(words []string, left int, right int) (ans int) {
123+
check := func(c byte) bool {
124+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
125+
}
126+
for _, w := range words[left : right+1] {
127+
if check(w[0]) && check(w[len(w)-1]) {
128+
ans++
129+
}
130+
}
131+
return
132+
}
133+
```
134+
135+
### **...**
136+
137+
```
138+
139+
```
140+
141+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# [2586. Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range)
2+
3+
[中文文档](/solution/2500-2599/2586.Count%20the%20Number%20of%20Vowel%20Strings%20in%20Range/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array of string <code>words</code> and two integers <code>left</code> and <code>right</code>.</p>
8+
9+
<p>A string is called a <strong>vowel string</strong> if it starts with a vowel character and ends with a vowel character where vowel characters are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>.</p>
10+
11+
<p>Return <em>the number of vowel strings </em><code>words[i]</code><em> where </em><code>i</code><em> belongs to the inclusive range </em><code>[left, right]</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> words = [&quot;are&quot;,&quot;amy&quot;,&quot;u&quot;], left = 0, right = 2
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong>
20+
- &quot;are&quot; is a vowel string because it starts with &#39;a&#39; and ends with &#39;e&#39;.
21+
- &quot;amy&quot; is not a vowel string because it does not end with a vowel.
22+
- &quot;u&quot; is a vowel string because it starts with &#39;u&#39; and ends with &#39;u&#39;.
23+
The number of vowel strings in the mentioned range is 2.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> words = [&quot;hey&quot;,&quot;aeo&quot;,&quot;mu&quot;,&quot;ooo&quot;,&quot;artro&quot;], left = 1, right = 4
30+
<strong>Output:</strong> 3
31+
<strong>Explanation:</strong>
32+
- &quot;aeo&quot; is a vowel string because it starts with &#39;a&#39; and ends with &#39;o&#39;.
33+
- &quot;mu&quot; is not a vowel string because it does not start with a vowel.
34+
- &quot;ooo&quot; is a vowel string because it starts with &#39;o&#39; and ends with &#39;o&#39;.
35+
- &quot;artro&quot; is a vowel string because it starts with &#39;a&#39; and ends with &#39;o&#39;.
36+
The number of vowel strings in the mentioned range is 3.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
44+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
45+
<li><code>words[i]</code> consists of only lowercase English letters.</li>
46+
<li><code>0 &lt;= left &lt;= right &lt; words.length</code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
class Solution:
57+
def vowelStrings(self, words: List[str], left: int, right: int) -> int:
58+
return sum(w[0] in 'aeiou' and w[-1] in 'aeiou' for w in words[left: right + 1])
59+
```
60+
61+
### **Java**
62+
63+
```java
64+
class Solution {
65+
public int vowelStrings(String[] words, int left, int right) {
66+
int ans = 0;
67+
for (int i = left; i <= right; ++i) {
68+
var w = words[i];
69+
if (check(w.charAt(0)) && check(w.charAt(w.length() - 1))) {
70+
++ans;
71+
}
72+
}
73+
return ans;
74+
}
75+
76+
private boolean check(char c) {
77+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int vowelStrings(vector<string>& words, int left, int right) {
88+
auto check = [](char c) -> bool {
89+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
90+
};
91+
int ans = 0;
92+
for (int i = left; i <= right; ++i) {
93+
auto w = words[i];
94+
ans += check(w[0]) && check(w[w.size() - 1]);
95+
}
96+
return ans;
97+
}
98+
};
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func vowelStrings(words []string, left int, right int) (ans int) {
105+
check := func(c byte) bool {
106+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
107+
}
108+
for _, w := range words[left : right+1] {
109+
if check(w[0]) && check(w[len(w)-1]) {
110+
ans++
111+
}
112+
}
113+
return
114+
}
115+
```
116+
117+
### **...**
118+
119+
```
120+
121+
```
122+
123+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int vowelStrings(vector<string>& words, int left, int right) {
4+
auto check = [](char c) -> bool {
5+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
6+
};
7+
int ans = 0;
8+
for (int i = left; i <= right; ++i) {
9+
auto w = words[i];
10+
ans += check(w[0]) && check(w[w.size() - 1]);
11+
}
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func vowelStrings(words []string, left int, right int) (ans int) {
2+
check := func(c byte) bool {
3+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
4+
}
5+
for _, w := range words[left : right+1] {
6+
if check(w[0]) && check(w[len(w)-1]) {
7+
ans++
8+
}
9+
}
10+
return
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int vowelStrings(String[] words, int left, int right) {
3+
int ans = 0;
4+
for (int i = left; i <= right; ++i) {
5+
var w = words[i];
6+
if (check(w.charAt(0)) && check(w.charAt(w.length() - 1))) {
7+
++ans;
8+
}
9+
}
10+
return ans;
11+
}
12+
13+
private boolean check(char c) {
14+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
15+
}
16+
}

0 commit comments

Comments
 (0)