Skip to content

Commit 6afc860

Browse files
yanglbmeidoocs
andauthored
feat: add biweekly contest 125 (#2404)
Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
1 parent 48e0a26 commit 6afc860

File tree

16 files changed

+720
-8
lines changed

16 files changed

+720
-8
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [3065. 超过阈值的最少操作数 I](https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-i)
2+
3+
[English Version](/solution/3000-3099/3065.Minimum%20Operations%20to%20Exceed%20Threshold%20Value%20I/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>给你一个下标从 <b>0</b>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。</p>
12+
13+
<p>一次操作中,你可以删除 <code>nums</code>&nbsp;中的最小元素。</p>
14+
15+
<p>你需要使数组中的所有元素都大于或等于 <code>k</code>&nbsp;,请你返回需要的 <strong>最少</strong>&nbsp;操作次数。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<b>输入:</b>nums = [2,11,10,1,3], k = 10
23+
<b>输出:</b>3
24+
<b>解释:</b>第一次操作后,nums 变为 [2, 11, 10, 3] 。
25+
第二次操作后,nums 变为 [11, 10, 3] 。
26+
第三次操作后,nums 变为 [11, 10] 。
27+
此时,数组中的所有元素都大于等于 10 ,所以我们停止操作。
28+
使数组中所有元素都大于等于 10 需要的最少操作次数为 3 。
29+
</pre>
30+
31+
<p><strong class="example">示例 2:</strong></p>
32+
33+
<pre>
34+
<b>输入:</b>nums = [1,1,2,4,9], k = 1
35+
<b>输出:</b>0
36+
<b>解释:</b>数组中的所有元素都大于等于 1 ,所以不需要对 nums 做任何操作。</pre>
37+
38+
<p><strong class="example">示例 3:</strong></p>
39+
40+
<pre>
41+
<b>输入:</b>nums = [1,1,2,4,9], k = 9
42+
<b>输出:</b>4
43+
<b>解释:</b>nums 中只有一个元素大于等于 9 ,所以需要执行 4 次操作。
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
52+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
53+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
54+
<li>输入保证至少有一个满足&nbsp;<code>nums[i] &gt;= k</code>&nbsp;的下标&nbsp;<code>i</code>&nbsp;存在。</li>
55+
</ul>
56+
57+
## 解法
58+
59+
### 方法一
60+
61+
<!-- tabs:start -->
62+
63+
```python
64+
65+
```
66+
67+
```java
68+
69+
```
70+
71+
```cpp
72+
73+
```
74+
75+
```go
76+
77+
```
78+
79+
<!-- tabs:end -->
80+
81+
<!-- end -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [3065. Minimum Operations to Exceed Threshold Value I](https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i)
2+
3+
[中文文档](/solution/3000-3099/3065.Minimum%20Operations%20to%20Exceed%20Threshold%20Value%20I/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
10+
11+
<p>In one operation, you can remove one occurrence of the smallest element of <code>nums</code>.</p>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of operations needed so that all elements of the array are greater than or equal to</em> <code>k</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [2,11,10,1,3], k = 10
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> After one operation, nums becomes equal to [2, 11, 10, 3].
22+
After two operations, nums becomes equal to [11, 10, 3].
23+
After three operations, nums becomes equal to [11, 10].
24+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
25+
It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [1,1,2,4,9], k = 1
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong> All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> nums = [1,1,2,4,9], k = 9
39+
<strong>Output:</strong> 4
40+
<strong>Explanation:</strong> only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
48+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
49+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
50+
<li>The input is generated such that there is at least one index <code>i</code> such that <code>nums[i] &gt;= k</code>.</li>
51+
</ul>
52+
53+
## Solutions
54+
55+
### Solution 1
56+
57+
<!-- tabs:start -->
58+
59+
```python
60+
61+
```
62+
63+
```java
64+
65+
```
66+
67+
```cpp
68+
69+
```
70+
71+
```go
72+
73+
```
74+
75+
<!-- tabs:end -->
76+
77+
<!-- end -->
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [3066. 超过阈值的最少操作数 II](https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-ii)
2+
3+
[English Version](/solution/3000-3099/3066.Minimum%20Operations%20to%20Exceed%20Threshold%20Value%20II/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。</p>
12+
13+
<p>一次操作中,你将执行:</p>
14+
15+
<ul>
16+
<li>选择 <code>nums</code>&nbsp;中最小的两个整数&nbsp;<code>x</code> 和&nbsp;<code>y</code>&nbsp;。</li>
17+
<li>将&nbsp;<code>x</code> 和&nbsp;<code>y</code> 从&nbsp;<code>nums</code>&nbsp;中删除。</li>
18+
<li>将&nbsp;<code>min(x, y) * 2 + max(x, y)</code>&nbsp;添加到数组中的任意位置。</li>
19+
</ul>
20+
21+
<p><b>注意,</b>只有当&nbsp;<code>nums</code>&nbsp;至少包含两个元素时,你才可以执行以上操作。</p>
22+
23+
<p>你需要使数组中的所有元素都大于或等于&nbsp;<code>k</code>&nbsp;,请你返回需要的&nbsp;<strong>最少</strong>&nbsp;操作次数。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<pre>
30+
<b>输入:</b>nums = [2,11,10,1,3], k = 10
31+
<b>输出:</b>2
32+
<b>解释:</b>第一次操作中,我们删除元素 1 和 2 ,然后添加 1 * 2 + 2 到 nums 中,nums 变为 [4, 11, 10, 3] 。
33+
第二次操作中,我们删除元素 3 和 4 ,然后添加 3 * 2 + 4 到 nums 中,nums 变为 [10, 11, 10] 。
34+
此时,数组中的所有元素都大于等于 10 ,所以我们停止操作。
35+
使数组中所有元素都大于等于 10 需要的最少操作次数为 2 。
36+
</pre>
37+
38+
<p><strong class="example">示例 2:</strong></p>
39+
40+
<pre>
41+
<b>输入:</b>nums = [1,1,2,4,9], k = 20
42+
<b>输出:</b>4
43+
<b>解释:</b>第一次操作后,nums 变为 [2, 4, 9, 3] 。
44+
第二次操作后,nums 变为 [7, 4, 9] 。
45+
第三次操作后,nums 变为 [15, 9] 。
46+
第四次操作后,nums 变为 [33] 。
47+
此时,数组中的所有元素都大于等于 20 ,所以我们停止操作。
48+
使数组中所有元素都大于等于 20 需要的最少操作次数为 4 。</pre>
49+
50+
<p>&nbsp;</p>
51+
52+
<p><strong>提示:</strong></p>
53+
54+
<ul>
55+
<li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
56+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
57+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
58+
<li>输入保证答案一定存在,也就是说一定存在一个操作序列使数组中所有元素都大于等于&nbsp;<code>k</code> 。</li>
59+
</ul>
60+
61+
## 解法
62+
63+
### 方法一
64+
65+
<!-- tabs:start -->
66+
67+
```python
68+
69+
```
70+
71+
```java
72+
73+
```
74+
75+
```cpp
76+
77+
```
78+
79+
```go
80+
81+
```
82+
83+
<!-- tabs:end -->
84+
85+
<!-- end -->
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [3066. Minimum Operations to Exceed Threshold Value II](https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii)
2+
3+
[中文文档](/solution/3000-3099/3066.Minimum%20Operations%20to%20Exceed%20Threshold%20Value%20II/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
10+
11+
<p>In one operation, you will:</p>
12+
13+
<ul>
14+
<li>Take the two smallest integers <code>x</code> and <code>y</code> in <code>nums</code>.</li>
15+
<li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li>
16+
<li>Add <code>min(x, y) * 2 + max(x, y)</code> anywhere in the array.</li>
17+
</ul>
18+
19+
<p><strong>Note</strong> that you can only apply the described operation if <code>nums</code> contains at least two elements.</p>
20+
21+
<p>Return <em>the <strong>minimum</strong> number of operations needed so that all elements of the array are greater than or equal to</em> <code>k</code>.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> nums = [2,11,10,1,3], k = 10
28+
<strong>Output:</strong> 2
29+
<strong>Explanation:</strong> In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
30+
In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
31+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
32+
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
33+
</pre>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> nums = [1,1,2,4,9], k = 20
39+
<strong>Output:</strong> 4
40+
<strong>Explanation:</strong> After one operation, nums becomes equal to [2, 4, 9, 3].
41+
After two operations, nums becomes equal to [7, 4, 9].
42+
After three operations, nums becomes equal to [15, 9].
43+
After four operations, nums becomes equal to [33].
44+
At this stage, all the elements of nums are greater than 20 so we can stop.
45+
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
52+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
53+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
54+
<li>The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to <code>k</code>.</li>
55+
</ul>
56+
57+
## Solutions
58+
59+
### Solution 1
60+
61+
<!-- tabs:start -->
62+
63+
```python
64+
65+
```
66+
67+
```java
68+
69+
```
70+
71+
```cpp
72+
73+
```
74+
75+
```go
76+
77+
```
78+
79+
<!-- tabs:end -->
80+
81+
<!-- end -->

0 commit comments

Comments
 (0)