Skip to content

Commit 01cf6af

Browse files
committed
chore: add new lc problems
1 parent 245d8d4 commit 01cf6af

File tree

23 files changed

+17768
-16365
lines changed

23 files changed

+17768
-16365
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
1-
# [2702. 使数字变为非正数的最小操作次数](https://leetcode.cn/problems/minimum-operations-to-make-numbers-non-positive)[English Version](/solution/2700-2799/2702.Minimum%20Operations%20to%20Make%20Numbers%20Non-positive/README_EN.md)## 题目描述<!-- 这里写题目描述 --><p>给定一个 <strong>下标从 0 开始</strong> 的整数数组 <code>nums</code>,以及两个整数 <code>x</code> 和 <code>y</code>。在每一次操作中,你需要选择一个满足条件 <code>0 &lt;= i &lt; nums.length</code> 的下标 <code>i</code>&nbsp;,并执行以下操作:</p><ul> <li>将 <code>nums[i]</code> 减去 <code>x</code>。</li> <li>将除了下标为 <code>i</code> 的位置外,其他位置的值都减去 <code>y</code>。</li></ul><p>返回使得 <code>nums</code> 中的所有整数都 <strong>小于等于零&nbsp;</strong>所需的最小操作次数。</p><p>&nbsp;</p><p><b>示例 1:</b></p><pre><b>输入:</b>nums = [3,4,1,7,6], x = 4, y = 2<b>输出:</b>3<b>解释:</b>你需要进行三次操作。其中一种最优操作序列如下:操作 1: 选择 i = 3。 然后, nums = [1,2,-1,3,4]. 操作 2: 选择 i = 3。 然后, nums = [-1,0,-3,-1,2].操作 3: 选择 i = 4。 然后, nums = [-3,-2,-5,-3,-2].现在,<code>nums</code> 中的所有数字都是非正数。因此,返回 3。</pre><p><strong class="example">示例 2:</strong></p><pre><b>输入:</b>nums = [1,2,1], x = 2, y = 1<b>输出:</b>1<b>解释:</b>我们可以在 <code>i = 1</code> 处执行一次操作,得到 <code>nums = [0,0,0]</code>。所有正数都被移除,因此返回 1。</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= y &lt; x &lt;= 10<sup>9</sup></code></li></ul>## 解法<!-- 这里可写通用的实现逻辑 --><!-- tabs:start -->### **Python3**<!-- 这里可写当前语言的特殊实现逻辑 -->`python`### **Java**<!-- 这里可写当前语言的特殊实现逻辑 -->`java`### **C++**`cpp`### **Go**`go`### **...**``````<!-- tabs:end -->
1+
# [2702. 使数字变为非正数的最小操作次数](https://leetcode.cn/problems/minimum-operations-to-make-numbers-non-positive)
2+
3+
[English Version](/solution/2700-2799/2702.Minimum%20Operations%20to%20Make%20Numbers%20Non-positive/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给定一个 <strong>下标从0开始</strong> 的整数数组 <code>nums</code>,以及两个整数 <code>x</code> 和 <code>y</code>。在每一次操作中,你需要选择一个满足条件 <code>0 &lt;= i &lt; nums.length</code> 的下标 <code>i</code>&nbsp;,并执行以下操作:</p>
10+
11+
<ul>
12+
<li>将 <code>nums[i]</code> 减去 <code>x</code>。</li>
13+
<li>将除了下标为 <code>i</code> 的位置外,其他位置的值都减去 <code>y</code>。</li>
14+
</ul>
15+
16+
<p>返回使得 <code>nums</code> 中的所有整数都 <strong>小于等于零&nbsp;</strong>所需的最小操作次数。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><b>示例 1:</b></p>
21+
22+
<pre>
23+
<b>输入:</b>nums = [3,4,1,7,6], x = 4, y = 2
24+
<b>输出:</b>3
25+
<b>解释:</b>你需要进行三次操作。其中一种最优操作序列如下:
26+
操作 1: 选择 i = 3。 然后, nums = [1,2,-1,3,4].
27+
操作 2: 选择 i = 3。 然后, nums = [-1,0,-3,-1,2].
28+
操作 3: 选择 i = 4。 然后, nums = [-3,-2,-5,-3,-2].
29+
现在,<code>nums</code> 中的所有数字都是非正数。因此,返回 3。
30+
</pre>
31+
32+
<p><strong class="example">示例 2:</strong></p>
33+
34+
<pre>
35+
<b>输入:</b>nums = [1,2,1], x = 2, y = 1
36+
<b>输出:</b>1
37+
<b>解释:</b>我们可以在 <code>i = 1</code> 处执行一次操作,得到 <code>nums = [0,0,0]</code>。所有正数都被移除,因此返回 1。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
46+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
47+
<li><code>1 &lt;= y &lt; x &lt;= 10<sup>9</sup></code></li>
48+
</ul>
49+
50+
## 解法
51+
52+
<!-- 这里可写通用的实现逻辑 -->
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
60+
```python
61+
62+
```
63+
64+
### **Java**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```java
69+
70+
```
71+
72+
### **C++**
73+
74+
```cpp
75+
76+
```
77+
78+
### **Go**
79+
80+
```go
81+
82+
```
83+
84+
### **...**
85+
86+
```
87+
88+
```
89+
90+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1 +1,80 @@
1-
# [2702. Minimum Operations to Make Numbers Non-positive](https://leetcode.com/problems/minimum-operations-to-make-numbers-non-positive)[中文文档](/solution/2700-2799/2702.Minimum%20Operations%20to%20Make%20Numbers%20Non-positive/README.md)## Description<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>x</code> and <code>y</code>. In one operation, you must choose an index <code>i</code> such that <code>0 &lt;= i &lt; nums.length</code> and perform the following:</p><ul> <li>Decrement <code>nums[i]</code> by <code>x</code>.</li> <li>Decrement values by <code>y</code> at all indices except the <code>i<sup>th</sup></code> one.</li></ul><p>Return <em>the minimum number of operations to make all the integers in </em><code>nums</code> <em><strong>less than or equal to zero.</strong></em></p><p>&nbsp;</p><p><strong class="example">Example 1:</strong></p><pre><strong>Input:</strong> nums = [3,4,1,7,6], x = 4, y = 2<strong>Output:</strong> 3<strong>Explanation:</strong> You will need three operations. One of the optimal sequence of operations is:Operation 1: Choose i = 3. Then, nums = [1,2,-1,3,4]. Operation 2: Choose i = 3. Then, nums = [-1,0,-3,-1,2].Operation 3: Choose i = 4. Then, nums = [-3,-2,-5,-3,-2].Now, all the numbers in nums are non-positive. Therefore, we return 3.</pre><p><strong class="example">Example 2:</strong></p><pre><strong>Input:</strong> nums = [1,2,1], x = 2, y = 1<strong>Output:</strong> 1<strong>Explanation:</strong> We can perform the operation once on i = 1. Then, nums becomes [0,0,0]. All the positive numbers are removed, and therefore, we return 1.</pre><p>&nbsp;</p><p><strong>Constraints:</strong></p><ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= y &lt; x &lt;= 10<sup>9</sup></code></li></ul>## Solutions<!-- tabs:start -->### **Python3**`python`### **Java**`java`### **C++**`cpp`### **Go**`go`### **...**``````<!-- tabs:end -->
1+
# [2702. Minimum Operations to Make Numbers Non-positive](https://leetcode.com/problems/minimum-operations-to-make-numbers-non-positive)
2+
3+
[中文文档](/solution/2700-2799/2702.Minimum%20Operations%20to%20Make%20Numbers%20Non-positive/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>x</code> and <code>y</code>. In one operation, you must choose an index <code>i</code> such that <code>0 &lt;= i &lt; nums.length</code> and perform the following:</p>
8+
9+
<ul>
10+
<li>Decrement <code>nums[i]</code> by <code>x</code>.</li>
11+
<li>Decrement values by <code>y</code> at all indices except the <code>i<sup>th</sup></code> one.</li>
12+
</ul>
13+
14+
<p>Return <em>the minimum number of operations to make all the integers in </em><code>nums</code> <em><strong>less than or equal to zero.</strong></em></p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [3,4,1,7,6], x = 4, y = 2
21+
<strong>Output:</strong> 3
22+
<strong>Explanation:</strong> You will need three operations. One of the optimal sequence of operations is:
23+
Operation 1: Choose i = 3. Then, nums = [1,2,-1,3,4].
24+
Operation 2: Choose i = 3. Then, nums = [-1,0,-3,-1,2].
25+
Operation 3: Choose i = 4. Then, nums = [-3,-2,-5,-3,-2].
26+
Now, all the numbers in nums are non-positive. Therefore, we return 3.
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [1,2,1], x = 2, y = 1
33+
<strong>Output:</strong> 1
34+
<strong>Explanation:</strong> We can perform the operation once on i = 1. Then, nums becomes [0,0,0]. All the positive numbers are removed, and therefore, we return 1.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
42+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
43+
<li><code>1 &lt;= y &lt; x &lt;= 10<sup>9</sup></code></li>
44+
</ul>
45+
46+
## Solutions
47+
48+
<!-- tabs:start -->
49+
50+
### **Python3**
51+
52+
```python
53+
54+
```
55+
56+
### **Java**
57+
58+
```java
59+
60+
```
61+
62+
### **C++**
63+
64+
```cpp
65+
66+
```
67+
68+
### **Go**
69+
70+
```go
71+
72+
```
73+
74+
### **...**
75+
76+
```
77+
78+
```
79+
80+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
1-
# [2714. 找到最短路径的 K 次跨越](https://leetcode.cn/problems/find-shortest-path-with-k-hops)[English Version](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README_EN.md)## 题目描述<!-- 这里写题目描述 --><p>现给定一个正整数 n ,它表示一个<strong>&nbsp;索引从 0 开始的无向带权连接图</strong> 的节点数,以及一个&nbsp;<strong>索引从 0 开始的二维数组&nbsp;</strong><code>edges</code> ,其中 <code>edges[i] = [ui, vi, wi]</code> 表示节点 <code>ui</code> 和 <code>vi</code> 之间存在权重为 <code>wi</code> 的边。</p><p>还给定两个节点 <code>s</code> 和 <code>d</code> ,以及一个正整数 <code>k</code> ,你的任务是找到从 s 到 d 的 <strong>最短 </strong>路径,但你可以 <strong>最多</strong> 跨越 <code>k</code> 条边。换句话说,将 <strong>最多</strong> <code>k</code> 条边的权重设为 <code>0</code>,然后找到从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径。</p><p>返回满足给定条件的从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径的长度。</p><p>&nbsp;</p><p><strong class="example">示例 1:</strong></p><pre><b>输入:</b>n = 4, edges = [[0,1,4],[0,2,2],[2,3,6]], s = 1, d = 3, k = 2<b>输出:</b>2<b>解释:</b>在这个例子中,只有一条从节点 1(绿色节点)到节点 3(红色节点)的路径,即(1-&gt;0-&gt;2-&gt;3),其长度为 4 + 2 + 6 = 12。现在我们可以将两条边的权重设为 0,即将蓝色边的权重设为 0,那么路径的长度就变为 0 + 2 + 0 = 2。可以证明 2 是我们在给定条件下能够达到的最小路径长度。</pre><p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/1.jpg" style="width: 170px; height: 171px;" /></p><p><strong class="example">示例 2:</strong></p><pre><b>输入:</b>n = 7, edges = [[3,1,9],[3,2,4],[4,0,9],[0,5,6],[3,6,2],[6,0,4],[1,2,4]], s = 4, d = 1, k = 2<b>输出:</b>6<b>解释:</b>在这个例子中,有两条从节点 4(绿色节点)到节点 1(红色节点)的路径,分别是(4-&gt;0-&gt;6-&gt;3-&gt;2-&gt;1)和(4-&gt;0-&gt;6-&gt;3-&gt;1)。第一条路径的长度为 9 + 4 + 2 + 4 + 4 = 23,第二条路径的长度为 9 + 4 + 2 + 9 = 24。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就变为 0 + 4 + 2 + 0 = 6。可以证明 6 是我们在给定条件下能够达到的最小路径长度。</pre><p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/2.jpg" style="width: 400px; height: 171px;" /></p><p><strong class="example">示例 3:</strong></p><pre><b>输入:</b>n = 5, edges = [[0,4,2],[0,1,3],[0,2,1],[2,1,4],[1,3,4],[3,4,7]], s = 2, d = 3, k = 1<b>输出:</b>3<b>解释:</b>在这个例子中,从节点 2(绿色节点)到节点 3(红色节点)有 4 条路径,分别是(2-&gt;1-&gt;3)、(2-&gt;0-&gt;1-&gt;3)、(2-&gt;1-&gt;0-&gt;4-&gt;3)和(2-&gt;0-&gt;4-&gt;3)。前两条路径的长度为 4 + 4 = 1 + 3 + 4 = 8,第三条路径的长度为 4 + 3 + 2 + 7 = 16,最后一条路径的长度为 1 + 2 + 7 = 10。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就为 1 + 2 + 0 = 3。可以证明在给定条件下,3 是我们能够达到的最小路径长度。</pre><p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/3.jpg" style="width: 300px; height: 296px;" /></p><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>2 &lt;= n &lt;= 500</code></li> <li><code>n - 1 &lt;= edges.length &lt;= n \* (n - 1) / 2</code></li> <li><code>edges[i].length = 3</code></li> <li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li> <li><code>1 &lt;= edges[i][2] &lt;=&nbsp;10<sup>6</sup></code></li> <li><code>0 &lt;= s, d, k&nbsp;&lt;= n - 1</code></li> <li><code>s != d</code></li> <li>输入的生成确保图是 <strong>连通</strong> 的,并且没有 <strong>重复的边</strong> 或 <strong>自环</strong>。</li></ul>## 解法<!-- 这里可写通用的实现逻辑 --><!-- tabs:start -->### **Python3**<!-- 这里可写当前语言的特殊实现逻辑 -->`python`### **Java**<!-- 这里可写当前语言的特殊实现逻辑 -->`java`### **C++**`cpp`### **Go**`go`### **...**``````<!-- tabs:end -->
1+
# [2714. 找到最短路径的 K 次跨越](https://leetcode.cn/problems/find-shortest-path-with-k-hops)
2+
3+
[English Version](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>现给定一个正整数 n ,它表示一个<strong>&nbsp;索引从 0 开始的无向带权连接图</strong> 的节点数,以及一个&nbsp;<strong>索引从 0 开始的二维数组&nbsp;</strong><code>edges</code> ,其中 <code>edges[i] = [ui, vi, wi]</code> 表示节点 <code>ui</code> 和 <code>vi</code> 之间存在权重为 <code>wi</code> 的边。</p>
10+
11+
<p>还给定两个节点 <code>s</code> 和 <code>d</code> ,以及一个正整数 <code>k</code> ,你的任务是找到从 s 到 d 的 <strong>最短 </strong>路径,但你可以 <strong>最多</strong> 跨越 <code>k</code> 条边。换句话说,将 <strong>最多</strong> <code>k</code> 条边的权重设为 <code>0</code>,然后找到从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径。</p>
12+
13+
<p>返回满足给定条件的从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径的长度。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<b>输入:</b>n = 4, edges = [[0,1,4],[0,2,2],[2,3,6]], s = 1, d = 3, k = 2
21+
<b>输出:</b>2
22+
<b>解释:</b>在这个例子中,只有一条从节点1(绿色节点)到节点3(红色节点)的路径,即(1-&gt;0-&gt;2-&gt;3),其长度为4 + 2 + 6 = 12。现在我们可以将两条边的权重设为 0,即将蓝色边的权重设为 0,那么路径的长度就变为 0 + 2 + 0 = 2。可以证明 2 是我们在给定条件下能够达到的最小路径长度。
23+
</pre>
24+
25+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/1.jpg" style="width: 170px; height: 171px;" /></p>
26+
27+
<p><strong class="example">示例 2:</strong></p>
28+
29+
<pre>
30+
<b>输入:</b>n = 7, edges = [[3,1,9],[3,2,4],[4,0,9],[0,5,6],[3,6,2],[6,0,4],[1,2,4]], s = 4, d = 1, k = 2
31+
<b>输出:</b>6
32+
<b>解释:</b>在这个例子中,有两条从节点4(绿色节点)到节点1(红色节点)的路径,分别是(4-&gt;0-&gt;6-&gt;3-&gt;2-&gt;1)和(4-&gt;0-&gt;6-&gt;3-&gt;1)。第一条路径的长度为 9 + 4 + 2 + 4 + 4 = 23,第二条路径的长度为 9 + 4 + 2 + 9 = 24。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就变为 0 + 4 + 2 + 0 = 6。可以证明 6 是我们在给定条件下能够达到的最小路径长度。
33+
</pre>
34+
35+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/2.jpg" style="width: 400px; height: 171px;" /></p>
36+
37+
<p><strong class="example">示例 3:</strong></p>
38+
39+
<pre>
40+
<b>输入:</b>n = 5, edges = [[0,4,2],[0,1,3],[0,2,1],[2,1,4],[1,3,4],[3,4,7]], s = 2, d = 3, k = 1
41+
<b>输出:</b>3
42+
<b>解释:</b>在这个例子中,从节点2(绿色节点)到节点3(红色节点)有4条路径,分别是(2-&gt;1-&gt;3)、(2-&gt;0-&gt;1-&gt;3)、(2-&gt;1-&gt;0-&gt;4-&gt;3)和(2-&gt;0-&gt;4-&gt;3)。前两条路径的长度为4 + 4 = 1 + 3 + 4 = 8,第三条路径的长度为4 + 3 + 2 + 7 = 16,最后一条路径的长度为1 + 2 + 7 = 10。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就为1 + 2 + 0 = 3。可以证明在给定条件下,3 是我们能够达到的最小路径长度。
43+
</pre>
44+
45+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/3.jpg" style="width: 300px; height: 296px;" /></p>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>2 &lt;= n &lt;= 500</code></li>
53+
<li><code>n - 1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li>
54+
<li><code>edges[i].length = 3</code></li>
55+
<li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li>
56+
<li><code>1 &lt;= edges[i][2] &lt;=&nbsp;10<sup>6</sup></code></li>
57+
<li><code>0 &lt;= s, d, k&nbsp;&lt;= n - 1</code></li>
58+
<li><code>s != d</code></li>
59+
<li>输入的生成确保图是 <strong>连通</strong> 的,并且没有 <strong>重复的边</strong> 或 <strong>自环</strong>。</li>
60+
</ul>
61+
62+
## 解法
63+
64+
<!-- 这里可写通用的实现逻辑 -->
65+
66+
<!-- tabs:start -->
67+
68+
### **Python3**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```python
73+
74+
```
75+
76+
### **Java**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```java
81+
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
94+
```
95+
96+
### **...**
97+
98+
```
99+
100+
```
101+
102+
<!-- tabs:end -->

0 commit comments

Comments
 (0)