Skip to content

Commit e0d002b

Browse files
committed
feat: add weekly contest 383
1 parent 745dd62 commit e0d002b

File tree

13 files changed

+698
-2
lines changed

13 files changed

+698
-2
lines changed

solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
<p><strong class="example">示例 2:</strong></p>
3737

38-
<p><strong class="example"><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images//1706880313-YelabI-example2.jpeg"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images//1706880313-YelabI-example2.jpeg" style="width: 900px; height: 250px;" /></a></strong></p>
38+
<p><strong class="example"><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images/1706880313-YelabI-example2.jpeg"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images/1706880313-YelabI-example2.jpeg" style="width: 900px; height: 250px;" /></a></strong></p>
3939

4040
<pre>
4141
<b>输入:</b>points = [[6,2],[4,4],[2,6]]
@@ -48,7 +48,7 @@
4848

4949
<p><strong class="example">示例 3:</strong></p>
5050

51-
<p><strong class="example"><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images//1706880311-mtPGYC-example3.jpeg"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images//1706880311-mtPGYC-example3.jpeg" style="width: 911px; height: 250px;" /></a></strong></p>
51+
<p><strong class="example"><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images/1706880311-mtPGYC-example3.jpeg"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/images/1706880311-mtPGYC-example3.jpeg" style="width: 911px; height: 250px;" /></a></strong></p>
5252

5353
<pre>
5454
<b>输入:</b>points = [[3,1],[1,3],[1,1]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [3028. 边界上的蚂蚁](https://leetcode.cn/problems/ant-on-the-boundary)
2+
3+
[English Version](/solution/3000-3099/3028.Ant%20on%20the%20Boundary/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>边界上有一只蚂蚁,它有时向 <strong>左 </strong>走,有时向 <strong>右 </strong>走。</p>
10+
11+
<p>给你一个 <strong>非零</strong> 整数数组 <code>nums</code> 。蚂蚁会按顺序读取 <code>nums</code> 中的元素,从第一个元素开始直到结束。每一步,蚂蚁会根据当前元素的值移动:</p>
12+
13+
<ul>
14+
<li>如果 <code>nums[i] &lt; 0</code> ,向 <strong>左</strong> 移动<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> <code>-nums[i]</code>单位。</li>
15+
<li>如果 <code>nums[i] &gt; 0</code> ,向 <strong>右</strong> 移动 <code>nums[i]</code>单位。</li>
16+
</ul>
17+
18+
<p>返回蚂蚁 <strong>返回 </strong>到边界上的次数。</p>
19+
20+
<p><strong>注意:</strong></p>
21+
22+
<ul>
23+
<li>边界两侧有无限的空间。</li>
24+
<li>只有在蚂蚁移动了 <code>|nums[i]|</code> 单位后才检查它是否位于边界上。换句话说,如果蚂蚁只是在移动过程中穿过了边界,则不会计算在内。<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd --></li>
25+
</ul>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong class="example">示例 1:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>nums = [2,3,-5]
33+
<strong>输出:</strong>1
34+
<strong>解释:</strong>第 1 步后,蚂蚁距边界右侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->
35+
第 2 步后,蚂蚁距边界右侧 5 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->
36+
第 3 步后,蚂蚁位于边界上。
37+
所以答案是 1 。
38+
</pre>
39+
40+
<p><strong class="example">示例 2:</strong></p>
41+
42+
<pre>
43+
<strong>输入:</strong>nums = [3,2,-3,-4]
44+
<strong>输出:</strong>0
45+
<strong>解释:</strong>第 1 步后,蚂蚁距边界右侧 3 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->
46+
第 2 步后,蚂蚁距边界右侧 5 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->
47+
第 3 步后,蚂蚁距边界右侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->
48+
第 4 步后,蚂蚁距边界左侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->
49+
蚂蚁从未返回到边界上,所以答案是 0 。
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
58+
<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
59+
<li><code>nums[i] != 0</code></li>
60+
</ul>
61+
62+
## 解法
63+
64+
### 方法一
65+
66+
<!-- tabs:start -->
67+
68+
```python
69+
70+
```
71+
72+
```java
73+
74+
```
75+
76+
```cpp
77+
78+
```
79+
80+
```go
81+
82+
```
83+
84+
<!-- tabs:end -->
85+
86+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [3028. Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary)
2+
3+
[中文文档](/solution/3000-3099/3028.Ant%20on%20the%20Boundary/README.md)
4+
5+
## Description
6+
7+
<p>An ant is on a boundary. It sometimes goes <strong>left</strong> and sometimes <strong>right</strong>.</p>
8+
9+
<p>You are given an array of <strong>non-zero</strong> integers <code>nums</code>. The ant starts reading <code>nums</code> from the first element of it to its end. At each step, it moves according to the value of the current element:</p>
10+
11+
<ul>
12+
<li>If <code>nums[i] &lt; 0</code>, it moves <strong>left</strong> by<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> <code>-nums[i]</code> units.</li>
13+
<li>If <code>nums[i] &gt; 0</code>, it moves <strong>right</strong> by <code>nums[i]</code> units.</li>
14+
</ul>
15+
16+
<p>Return <em>the number of times the ant <strong>returns</strong> to the boundary.</em></p>
17+
18+
<p><strong>Notes:</strong></p>
19+
20+
<ul>
21+
<li>There is an infinite space on both sides of the boundary.</li>
22+
<li>We check whether the ant is on the boundary only after it has moved <code>|nums[i]|</code> units. In other words, if the ant crosses the boundary during its movement, it does not count.<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd --></li>
23+
</ul>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [2,3,-5]
30+
<strong>Output:</strong> 1
31+
<strong>Explanation:</strong> After the first step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
32+
After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
33+
After the third step, the ant is on the boundary.
34+
So the answer is 1.
35+
</pre>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> nums = [3,2,-3,-4]
41+
<strong>Output:</strong> 0
42+
<strong>Explanation:</strong> After the first step, the ant is 3 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
43+
After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
44+
After the third step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
45+
After the fourth step, the ant is 2 steps to the left of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
46+
The ant never returned to the boundary, so the answer is 0.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
54+
<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
55+
<li><code>nums[i] != 0</code></li>
56+
</ul>
57+
58+
## Solutions
59+
60+
### Solution 1
61+
62+
<!-- tabs:start -->
63+
64+
```python
65+
66+
```
67+
68+
```java
69+
70+
```
71+
72+
```cpp
73+
74+
```
75+
76+
```go
77+
78+
```
79+
80+
<!-- tabs:end -->
81+
82+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# [3029. 将单词恢复初始状态所需的最短时间 I](https://leetcode.cn/problems/minimum-time-to-revert-word-to-initial-state-i)
2+
3+
[English Version](/solution/3000-3099/3029.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 和一个整数 <code>k</code> 。</p>
10+
11+
<p>在每一秒,你必须执行以下操作:</p>
12+
13+
<ul>
14+
<li>移除 <code>word</code> 的前 <code>k</code> 个字符。</li>
15+
<li>在 <code>word</code> 的末尾添加 <code>k</code> 个任意字符。</li>
16+
</ul>
17+
18+
<p><strong>注意 </strong>添加的字符不必和移除的字符相同。但是,必须在每一秒钟都执行 <strong>两种 </strong>操作。</p>
19+
20+
<p>返回将 <code>word</code> 恢复到其 <strong>初始 </strong>状态所需的 <strong>最短 </strong>时间(该时间必须大于零)。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>word = "abacaba", k = 3
28+
<strong>输出:</strong>2
29+
<strong>解释:</strong>
30+
第 1 秒,移除 word 的前缀 "aba",并在末尾添加 "bac" 。因此,word 变为 "cababac"。
31+
第 2 秒,移除 word 的前缀 "cab",并在末尾添加 "aba" 。因此,word 变为 "abacaba" 并恢复到始状态。
32+
可以证明,2 秒是 word 恢复到其初始状态所需的最短时间。
33+
</pre>
34+
35+
<p><strong class="example">示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>word = "abacaba", k = 4
39+
<strong>输出:</strong>1
40+
<strong>解释:
41+
</strong>第 1 秒,移除 word 的前缀 "abac",并在末尾添加 "caba" 。因此,word 变为 "abacaba" 并恢复到初始状态。
42+
可以证明,1 秒是 word 恢复到其初始状态所需的最短时间。
43+
</pre>
44+
45+
<p><strong class="example">示例 3:</strong></p>
46+
47+
<pre>
48+
<strong>输入:</strong>word = "abcbabcd", k = 2
49+
<strong>输出:</strong>4
50+
<strong>解释:</strong>
51+
每一秒,我们都移除 word 的前 2 个字符,并在 word 末尾添加相同的字符。
52+
4 秒后,word 变为 "abcbabcd" 并恢复到初始状态。
53+
可以证明,4 秒是 word 恢复到其初始状态所需的最短时间。
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
58+
<p><strong>提示:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= word.length &lt;= 50</code></li>
62+
<li><code>1 &lt;= k &lt;= word.length</code></li>
63+
<li><code>word</code>仅由小写英文字母组成。</li>
64+
</ul>
65+
66+
## 解法
67+
68+
### 方法一
69+
70+
<!-- tabs:start -->
71+
72+
```python
73+
74+
```
75+
76+
```java
77+
78+
```
79+
80+
```cpp
81+
82+
```
83+
84+
```go
85+
86+
```
87+
88+
<!-- tabs:end -->
89+
90+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [3029. Minimum Time to Revert Word to Initial State I](https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-i)
2+
3+
[中文文档](/solution/3000-3099/3029.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> string <code>word</code> and an integer <code>k</code>.</p>
8+
9+
<p>At every second, you must perform the following operations:</p>
10+
11+
<ul>
12+
<li>Remove the first <code>k</code> characters of <code>word</code>.</li>
13+
<li>Add any <code>k</code> characters to the end of <code>word</code>.</li>
14+
</ul>
15+
16+
<p><strong>Note</strong> that you do not necessarily need to add the same characters that you removed. However, you must perform <strong>both</strong> operations at every second.</p>
17+
18+
<p>Return <em>the <strong>minimum</strong> time greater than zero required for</em> <code>word</code> <em>to revert to its <strong>initial</strong> state</em>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> word = &quot;abacaba&quot;, k = 3
25+
<strong>Output:</strong> 2
26+
<strong>Explanation:</strong> At the 1st second, we remove characters &quot;aba&quot; from the prefix of word, and add characters &quot;bac&quot; to the end of word. Thus, word becomes equal to &quot;cababac&quot;.
27+
At the 2nd second, we remove characters &quot;cab&quot; from the prefix of word, and add &quot;aba&quot; to the end of word. Thus, word becomes equal to &quot;abacaba&quot; and reverts to its initial state.
28+
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
29+
</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> word = &quot;abacaba&quot;, k = 4
35+
<strong>Output:</strong> 1
36+
<strong>Explanation:</strong> At the 1st second, we remove characters &quot;abac&quot; from the prefix of word, and add characters &quot;caba&quot; to the end of word. Thus, word becomes equal to &quot;abacaba&quot; and reverts to its initial state.
37+
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> word = &quot;abcbabcd&quot;, k = 2
44+
<strong>Output:</strong> 4
45+
<strong>Explanation:</strong> At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
46+
After 4 seconds, word becomes equal to &quot;abcbabcd&quot; and reverts to its initial state.
47+
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= word.length &lt;= 50 </code></li>
55+
<li><code>1 &lt;= k &lt;= word.length</code></li>
56+
<li><code>word</code> consists only of lowercase English letters.</li>
57+
</ul>
58+
59+
## Solutions
60+
61+
### Solution 1
62+
63+
<!-- tabs:start -->
64+
65+
```python
66+
67+
```
68+
69+
```java
70+
71+
```
72+
73+
```cpp
74+
75+
```
76+
77+
```go
78+
79+
```
80+
81+
<!-- tabs:end -->
82+
83+
<!-- end -->

0 commit comments

Comments
 (0)