Skip to content

Commit a72c20a

Browse files
committed
feat: add solutions to lc problems: No.2399~2402
* No.2399.Check Distances Between Same Letters * No.2400.Number of Ways to Reach a Position After Exactly k Steps * No.2401.Longest Nice Subarray * No.2402.Meeting Rooms III
1 parent e344957 commit a72c20a

File tree

37 files changed

+4220
-2395
lines changed

37 files changed

+4220
-2395
lines changed

solution/1800-1899/1882.Process Tasks Using Servers/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65-
优先队列”实现。
65+
**方法一:优先队列(小根堆)**
6666

6767
定义两个优先级队列,分别表示空闲服务器、使用中的服务器。其中:空闲服务器 `idle` 依据**权重、下标**排序;而使用中的服务器 `busy` 依据**结束时间、权重、下标**排序。
6868

@@ -72,6 +72,8 @@
7272
- 若当前有空闲服务器,那么在空闲队列 `idle` 中取出权重最小的服务器,将其加入使用中的队列 `busy` 中;
7373
- 若当前没有空闲服务器,那么在使用队列 `busy` 中找出最早结束时间且权重最小的服务器,重新加入使用中的队列 `busy` 中。
7474

75+
相似题目:[2402. 会议室 III](/solution/2400-2499/2402.Meeting%20Rooms%20III/README.md)
76+
7577
<!-- tabs:start -->
7678

7779
### **Python3**

solution/2300-2399/2395.Find Subarrays With Equal Sum/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
4646
</ul>
4747

48-
4948
## 解法
5049

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

solution/2300-2399/2395.Find Subarrays With Equal Sum/README_EN.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ Note that even though the subarrays have the same content, the two subarrays are
4444
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
4545
</ul>
4646

47-
4847
## Solutions
4948

5049
<!-- tabs:start -->

solution/2300-2399/2396.Strictly Palindromic Number/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<li><code>4 &lt;= n &lt;= 10<sup>5</sup></code></li>
4141
</ul>
4242

43-
4443
## 解法
4544

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

solution/2300-2399/2396.Strictly Palindromic Number/README_EN.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Therefore, we return false.
3939
<li><code>4 &lt;= n &lt;= 10<sup>5</sup></code></li>
4040
</ul>
4141

42-
4342
## Solutions
4443

4544
<!-- tabs:start -->

solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<p><strong>示例 1:</strong></p>
1818

19-
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered.png" style="width: 250px; height: 417px;"></strong></p>
19+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2397.Maximum%20Rows%20Covered%20by%20Columns/images/rowscovered.png" style="width: 250px; height: 417px;"></strong></p>
2020

2121
<pre><b>输入:</b>mat = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], cols = 2
2222
<b>输出:</b>3
@@ -27,7 +27,7 @@
2727

2828
<p><strong>示例 2:</strong></p>
2929

30-
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered2.png" style="width: 83px; height: 247px;"></strong></p>
30+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2397.Maximum%20Rows%20Covered%20by%20Columns/images/rowscovered2.png" style="width: 83px; height: 247px;"></strong></p>
3131

3232
<pre><b>输入:</b>mat = [[1],[0]], cols = 1
3333
<b>输出:</b>2
@@ -48,7 +48,6 @@
4848
<li><code>1 &lt;= cols &lt;= n</code></li>
4949
</ul>
5050

51-
5251
## 解法
5352

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

solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<p>&nbsp;</p>
1414
<p><strong>Example 1:</strong></p>
1515

16-
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered.png" style="width: 250px; height: 417px;" /></strong></p>
16+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2397.Maximum%20Rows%20Covered%20by%20Columns/images/rowscovered.png" style="width: 250px; height: 417px;" /></strong></p>
1717

1818
<pre>
1919
<strong>Input:</strong> mat = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], cols = 2
@@ -25,7 +25,7 @@ It can be shown that no more than 3 rows can be covered, so we return 3.
2525

2626
<p><strong>Example 2:</strong></p>
2727

28-
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered2.png" style="width: 83px; height: 247px;" /></strong></p>
28+
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2397.Maximum%20Rows%20Covered%20by%20Columns/images/rowscovered2.png" style="width: 83px; height: 247px;" /></strong></p>
2929

3030
<pre>
3131
<strong>Input:</strong> mat = [[1],[0]], cols = 1
@@ -46,7 +46,6 @@ Therefore, we return 2.
4646
<li><code>1 &lt;= cols &lt;= n</code></li>
4747
</ul>
4848

49-
5049
## Solutions
5150

5251
<!-- tabs:start -->

solution/2300-2399/2398.Maximum Number of Robots Within Budget/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
<li><code>1 &lt;= budget &lt;= 10<sup>15</sup></code></li>
4545
</ul>
4646

47-
4847
## 解法
4948

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

solution/2300-2399/2398.Maximum Number of Robots Within Budget/README_EN.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ It can be shown that it is not possible to run more than 3 consecutive robots wi
4040
<li><code>1 &lt;= budget &lt;= 10<sup>15</sup></code></li>
4141
</ul>
4242

43-
4443
## Solutions
4544

4645
<!-- tabs:start -->
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# [2399. 检查相同字母间的距离](https://leetcode.cn/problems/check-distances-between-same-letters)
2+
3+
[English Version](/solution/2300-2399/2399.Check%20Distances%20Between%20Same%20Letters/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ,该字符串仅由小写英文字母组成,<code>s</code> 中的每个字母都 <strong>恰好</strong> 出现 <strong>两次</strong> 。另给你一个下标从 <strong>0</strong> 开始、长度为 <code>26</code> 的的整数数组 <code>distance</code> 。</p>
10+
11+
<p>字母表中的每个字母按从 <code>0</code> 到 <code>25</code> 依次编号(即,<code>'a' -&gt; 0</code>, <code>'b' -&gt; 1</code>, <code>'c' -&gt; 2</code>, ... , <code>'z' -&gt; 25</code>)。</p>
12+
13+
<p>在一个 <strong>匀整</strong> 字符串中,第 <code>i</code> 个字母的两次出现之间的字母数量是 <code>distance[i]</code> 。如果第 <code>i</code> 个字母没有在 <code>s</code> 中出现,那么 <code>distance[i]</code> 可以 <strong>忽略</strong> 。</p>
14+
15+
<p>如果 <code>s</code> 是一个 <strong>匀整</strong> 字符串,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre><strong>输入:</strong>s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
22+
<strong>输出:</strong>true
23+
<strong>解释:</strong>
24+
- 'a' 在下标 0 和下标 2 处出现,所以满足 distance[0] = 1 。
25+
- 'b' 在下标 1 和下标 5 处出现,所以满足 distance[1] = 3 。
26+
- 'c' 在下标 3 和下标 4 处出现,所以满足 distance[2] = 0 。
27+
注意 distance[3] = 5 ,但是由于 'd' 没有在 s 中出现,可以忽略。
28+
因为 s 是一个匀整字符串,返回 true 。
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre><strong>输入:</strong>s = "aa", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
34+
<strong>输出:</strong>false
35+
<strong>解释:</strong>
36+
- 'a' 在下标 0 和 1 处出现,所以两次出现之间的字母数量为 0 。
37+
但是 distance[0] = 1 ,s 不是一个匀整字符串。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>2 &lt;= s.length &lt;= 52</code></li>
46+
<li><code>s</code> 仅由小写英文字母组成</li>
47+
<li><code>s</code> 中的每个字母恰好出现两次</li>
48+
<li><code>distance.length == 26</code></li>
49+
<li><code>0 &lt;= distance[i] &lt;= 50</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
**方法一:哈希表**
57+
58+
用哈希表记录每个字母出现的下标,然后遍历哈希表,判断每个字母的下标之差是否等于 `distance` 中对应的值。
59+
60+
时间复杂度 $O(n)$,其中 $n$ 为字符串 `s` 的长度。
61+
62+
<!-- tabs:start -->
63+
64+
### **Python3**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```python
69+
class Solution:
70+
def checkDistances(self, s: str, distance: List[int]) -> bool:
71+
d = [0] * 26
72+
for i, c in enumerate(s):
73+
j = ord(c) - ord("a")
74+
if d[j] and i - d[j] != distance[j]:
75+
return False
76+
d[j] = i + 1
77+
return True
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
class Solution {
86+
public boolean checkDistances(String s, int[] distance) {
87+
int[] d = new int[26];
88+
for (int i = 0; i < s.length(); ++i) {
89+
int j = s.charAt(i) - 'a';
90+
if (d[j] > 0 && i - d[j] != distance[j]) {
91+
return false;
92+
}
93+
d[j] = i + 1;
94+
}
95+
return true;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
bool checkDistances(string s, vector<int>& distance) {
106+
vector<int> d(26);
107+
for (int i = 0; i < s.size(); ++i) {
108+
int j = s[i] - 'a';
109+
if (d[j] && i - d[j] != distance[j]) {
110+
return false;
111+
}
112+
d[j] = i + 1;
113+
}
114+
return true;
115+
}
116+
};
117+
```
118+
119+
### **Go**
120+
121+
```go
122+
func checkDistances(s string, distance []int) bool {
123+
d := make([]int, 26)
124+
for i, c := range s {
125+
j := c - 'a'
126+
if d[j] > 0 && i-d[j] != distance[j] {
127+
return false
128+
}
129+
d[j] = i + 1
130+
}
131+
return true
132+
}
133+
```
134+
135+
### **TypeScript**
136+
137+
```ts
138+
139+
```
140+
141+
### **...**
142+
143+
```
144+
145+
146+
```
147+
148+
<!-- tabs:end -->

0 commit comments

Comments
 (0)