Skip to content

Commit f284d91

Browse files
committed
feat: add new lc problems
1 parent 547caf7 commit f284d91

File tree

16 files changed

+780
-0
lines changed

16 files changed

+780
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [2855. 使数组成为递增数组的最少右移次数](https://leetcode.cn/problems/minimum-right-shifts-to-sort-the-array)
2+
3+
[English Version](/solution/2800-2899/2855.Minimum%20Right%20Shifts%20to%20Sort%20the%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>nums</code>&nbsp;,数组中的元素为&nbsp;<strong>互不相同</strong>&nbsp;的正整数。请你返回让 <code>nums</code>&nbsp;成为递增数组的 <strong>最少右移</strong>&nbsp;次数,如果无法得到递增数组,返回 <code>-1</code>&nbsp;。</p>
10+
11+
<p>一次 <strong>右移</strong>&nbsp;指的是同时对所有下标进行操作,将下标为 <code>i</code>&nbsp;的元素移动到下标&nbsp;<code>(i + 1) % n</code>&nbsp;处。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong class="example">示例 1:</strong></p>
16+
17+
<pre>
18+
<b>输入:</b>nums = [3,4,5,1,2]
19+
<b>输出:</b>2
20+
<b>解释:</b>
21+
第一次右移后,nums = [2,3,4,5,1] 。
22+
第二次右移后,nums = [1,2,3,4,5] 。
23+
现在 nums 是递增数组了,所以答案为 2 。
24+
</pre>
25+
26+
<p><strong class="example">示例 2:</strong></p>
27+
28+
<pre>
29+
<b>输入:</b>nums = [1,3,5]
30+
<b>输出:</b>0
31+
<b>解释:</b>nums 已经是递增数组了,所以答案为 0 。</pre>
32+
33+
<p><strong class="example">示例 3:</strong></p>
34+
35+
<pre>
36+
<b>输入:</b>nums = [2,1,4]
37+
<b>输出:</b>-1
38+
<b>解释:</b>无法将数组变为递增数组。
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
48+
<li><code>nums</code>&nbsp;中的整数互不相同。</li>
49+
</ul>
50+
51+
## 解法
52+
53+
<!-- 这里可写通用的实现逻辑 -->
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
63+
```
64+
65+
### **Java**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```java
70+
71+
```
72+
73+
### **C++**
74+
75+
```cpp
76+
77+
```
78+
79+
### **Go**
80+
81+
```go
82+
83+
```
84+
85+
### **...**
86+
87+
```
88+
89+
```
90+
91+
<!-- tabs:end -->
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [2855. Minimum Right Shifts to Sort the Array](https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array)
2+
3+
[中文文档](/solution/2800-2899/2855.Minimum%20Right%20Shifts%20to%20Sort%20the%20Array/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code> containing <strong>distinct</strong> positive integers. Return <em>the <strong>minimum</strong> number of <strong>right shifts</strong> required to sort </em><code>nums</code><em> and </em><code>-1</code><em> if this is not possible.</em></p>
8+
9+
<p>A <strong>right shift</strong> is defined as shifting the element at index <code>i</code> to index <code>(i + 1) % n</code>, for all indices.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [3,4,5,1,2]
16+
<strong>Output:</strong> 2
17+
<strong>Explanation:</strong>
18+
After the first right shift, nums = [2,3,4,5,1].
19+
After the second right shift, nums = [1,2,3,4,5].
20+
Now nums is sorted; therefore the answer is 2.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,3,5]
27+
<strong>Output:</strong> 0
28+
<strong>Explanation:</strong> nums is already sorted therefore, the answer is 0.</pre>
29+
30+
<p><strong class="example">Example 3:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> nums = [2,1,4]
34+
<strong>Output:</strong> -1
35+
<strong>Explanation:</strong> It&#39;s impossible to sort the array using right shifts.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
43+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
44+
<li><code>nums</code> contains distinct integers.</li>
45+
</ul>
46+
47+
## Solutions
48+
49+
<!-- tabs:start -->
50+
51+
### **Python3**
52+
53+
```python
54+
55+
```
56+
57+
### **Java**
58+
59+
```java
60+
61+
```
62+
63+
### **C++**
64+
65+
```cpp
66+
67+
```
68+
69+
### **Go**
70+
71+
```go
72+
73+
```
74+
75+
### **...**
76+
77+
```
78+
79+
```
80+
81+
<!-- tabs:end -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# [2856. 删除数对后的最小数组长度](https://leetcode.cn/problems/minimum-array-length-after-pair-removals)
2+
3+
[English Version](/solution/2800-2899/2856.Minimum%20Array%20Length%20After%20Pair%20Removals/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的 <strong>非递减</strong> 整数数组&nbsp;<code>nums</code>&nbsp;。</p>
10+
11+
<p>你可以执行以下操作任意次:</p>
12+
13+
<ul>
14+
<li>选择 <strong>两个&nbsp;</strong>下标&nbsp;<code>i</code> 和&nbsp;<code>j</code>&nbsp;,满足&nbsp;<code>i &lt; j</code>&nbsp;且&nbsp;<code>nums[i] &lt; nums[j]</code>&nbsp;。</li>
15+
<li>将 <code>nums</code>&nbsp;中下标在&nbsp;<code>i</code> 和&nbsp;<code>j</code>&nbsp;处的元素删除。剩余元素按照原来的顺序组成新的数组,下标也重新从 <strong>0</strong>&nbsp;开始编号。</li>
16+
</ul>
17+
18+
<p>请你返回一个整数,表示执行以上操作任意次后(可以执行 <strong>0</strong> 次),<code>nums</code>&nbsp;数组的 <strong>最小</strong>&nbsp;数组长度。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong class="example">示例 1:</strong></p>
23+
24+
<pre>
25+
<b>输入:</b>nums = [1,3,4,9]
26+
<b>输出:</b>0
27+
<b>解释:</b>一开始,nums = [1, 3, 4, 9] 。
28+
第一次操作,我们选择下标 0 和 1 ,满足 nums[0] &lt; nums[1] &lt;=&gt; 1 &lt; 3 。
29+
删除下标 0 和 1 处的元素,nums 变成 [4, 9] 。
30+
下一次操作,我们选择下标 0 和 1 ,满足 nums[0] &lt; nums[1] &lt;=&gt; 4 &lt; 9 。
31+
删除下标 0 和 1 处的元素,nums 变成空数组 [] 。
32+
所以,可以得到的最小数组长度为 0 。</pre>
33+
34+
<p><strong class="example">示例 2:</strong></p>
35+
36+
<pre>
37+
<b>输入:</b>nums = [2,3,6,9]
38+
<b>输出:</b>0
39+
<b>解释:</b>一开始,nums = [2, 3, 6, 9] 。
40+
第一次操作,我们选择下标 0 和 2 ,满足 nums[0] &lt; nums[2] &lt;=&gt; 2 &lt; 6 。
41+
删除下标 0 和 2 处的元素,nums 变成 [3, 9] 。
42+
下一次操作,我们选择下标 0 和 1 ,满足 nums[0] &lt; nums[1] &lt;=&gt; 3 &lt; 9 。
43+
删除下标 0 和 1 处的元素,nums 变成空数组 [] 。
44+
所以,可以得到的最小数组长度为 0 。
45+
</pre>
46+
47+
<p><strong class="example">示例 3:</strong></p>
48+
49+
<pre>
50+
<b>输入:</b>nums = [1,1,2]
51+
<b>输出:</b>1
52+
<b>解释:</b>一开始,nums = [1, 1, 2] 。
53+
第一次操作,我们选择下标 0 和 2 ,满足 nums[0] &lt; nums[2] &lt;=&gt; 1 &lt; 2 。
54+
删除下标 0 和 2 处的元素,nums 变成 [1] 。
55+
无法对数组再执行操作。
56+
所以,可以得到的最小数组长度为 1 。
57+
</pre>
58+
59+
<p>&nbsp;</p>
60+
61+
<p><strong>提示:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
65+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
66+
<li><code>nums</code>&nbsp;是 <strong>非递减</strong>&nbsp;数组。</li>
67+
</ul>
68+
69+
## 解法
70+
71+
<!-- 这里可写通用的实现逻辑 -->
72+
73+
<!-- tabs:start -->
74+
75+
### **Python3**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```python
80+
81+
```
82+
83+
### **Java**
84+
85+
<!-- 这里可写当前语言的特殊实现逻辑 -->
86+
87+
```java
88+
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
101+
```
102+
103+
### **...**
104+
105+
```
106+
107+
```
108+
109+
<!-- tabs:end -->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2856. Minimum Array Length After Pair Removals](https://leetcode.com/problems/minimum-array-length-after-pair-removals)
2+
3+
[中文文档](/solution/2800-2899/2856.Minimum%20Array%20Length%20After%20Pair%20Removals/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> <strong>sorted</strong> array of integers <code>nums</code>.</p>
8+
9+
<p>You can perform the following operation any number of times:</p>
10+
11+
<ul>
12+
<li>Choose <strong>two</strong> indices, <code>i</code> and <code>j</code>, where <code>i &lt; j</code>, such that <code>nums[i] &lt; nums[j]</code>.</li>
13+
<li>Then, remove the elements at indices <code>i</code> and <code>j</code> from <code>nums</code>. The remaining elements retain their original order, and the array is re-indexed.</li>
14+
</ul>
15+
16+
<p>Return <em>an integer that denotes the <strong>minimum</strong> length of </em><code>nums</code><em> after performing the operation any number of times (<strong>including zero</strong>).</em></p>
17+
18+
<p>Note that <code>nums</code> is sorted in <strong>non-decreasing</strong> order.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> nums = [1,3,4,9]
25+
<strong>Output:</strong> 0
26+
<strong>Explanation:</strong> Initially, nums = [1, 3, 4, 9].
27+
In the first operation, we can choose index 0 and 1 because nums[0] &lt; nums[1] &lt;=&gt; 1 &lt; 3.
28+
Remove indices 0 and 1, and nums becomes [4, 9].
29+
For the next operation, we can choose index 0 and 1 because nums[0] &lt; nums[1] &lt;=&gt; 4 &lt; 9.
30+
Remove indices 0 and 1, and nums becomes an empty array [].
31+
Hence, the minimum length achievable is 0.</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums = [2,3,6,9]
37+
<strong>Output:</strong> 0
38+
<strong>Explanation:</strong> Initially, nums = [2, 3, 6, 9].
39+
In the first operation, we can choose index 0 and 2 because nums[0] &lt; nums[2] &lt;=&gt; 2 &lt; 6.
40+
Remove indices 0 and 2, and nums becomes [3, 9].
41+
For the next operation, we can choose index 0 and 1 because nums[0] &lt; nums[1] &lt;=&gt; 3 &lt; 9.
42+
Remove indices 0 and 1, and nums becomes an empty array [].
43+
Hence, the minimum length achievable is 0.
44+
</pre>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<pre>
49+
<strong>Input:</strong> nums = [1,1,2]
50+
<strong>Output:</strong> 1
51+
<strong>Explanation:</strong> Initially, nums = [1, 1, 2].
52+
In an operation, we can choose index 0 and 2 because nums[0] &lt; nums[2] &lt;=&gt; 1 &lt; 2.
53+
Remove indices 0 and 2, and nums becomes [1].
54+
It is no longer possible to perform an operation on the array.
55+
Hence, the minimum achievable length is 1.
56+
</pre>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
63+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
64+
<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
65+
</ul>
66+
67+
## Solutions
68+
69+
<!-- tabs:start -->
70+
71+
### **Python3**
72+
73+
```python
74+
75+
```
76+
77+
### **Java**
78+
79+
```java
80+
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
93+
```
94+
95+
### **...**
96+
97+
```
98+
99+
```
100+
101+
<!-- tabs:end -->

0 commit comments

Comments
 (0)