Skip to content

Commit bc4695b

Browse files
yanglbmeidoocs
andauthored
feat: add new lc problems: No.2873~2876 (#1733)
* No.2873.Maximum Value of an Ordered Triplet I * No.2874.Maximum Value of an Ordered Triplet II * No.2875.Minimum Size Subarray in Infinite Array * No.2876.Count Visited Nodes in a Directed Graph --------- Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
1 parent 5f59e4a commit bc4695b

File tree

16 files changed

+737
-0
lines changed

16 files changed

+737
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2873. 有序三元组中的最大值 I](https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-i)
2+
3+
[English Version](/solution/2800-2899/2873.Maximum%20Value%20of%20an%20Ordered%20Triplet%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
10+
11+
<p>请你从所有满足&nbsp;<code>i &lt; j &lt; k</code> 的下标三元组 <code>(i, j, k)</code> 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 <code>0</code> 。</p>
12+
13+
<p><strong>下标三元组</strong> <code>(i, j, k)</code> 的值等于 <code>(nums[i] - nums[j]) * nums[k]</code> 。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>nums = [12,6,1,2,7]
21+
<strong>输出:</strong>77
22+
<strong>解释:</strong>下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。
23+
可以证明不存在值大于 77 的有序下标三元组。
24+
</pre>
25+
26+
<p><strong class="example">示例 2:</strong></p>
27+
28+
<pre>
29+
<strong>输入:</strong>nums = [1,10,3,4,19]
30+
<strong>输出:</strong>133
31+
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
32+
可以证明不存在值大于 133 的有序下标三元组。
33+
</pre>
34+
35+
<p><strong class="example">示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>nums = [1,2,3]
39+
<strong>输出:</strong>0
40+
<strong>解释:</strong>唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```java
71+
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
78+
```
79+
80+
### **Go**
81+
82+
```go
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [2873. Maximum Value of an Ordered Triplet I](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i)
2+
3+
[中文文档](/solution/2800-2899/2873.Maximum%20Value%20of%20an%20Ordered%20Triplet%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
8+
9+
<p>Return <em><strong>the maximum value over all triplets of indices</strong></em> <code>(i, j, k)</code> <em>such that</em> <code>i &lt; j &lt; k</code>. If all such triplets have a negative value, return <code>0</code>.</p>
10+
11+
<p>The <strong>value of a triplet of indices</strong> <code>(i, j, k)</code> is equal to <code>(nums[i] - nums[j]) * nums[k]</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [12,6,1,2,7]
18+
<strong>Output:</strong> 77
19+
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
20+
It can be shown that there are no ordered triplets of indices with a value greater than 77.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,10,3,4,19]
27+
<strong>Output:</strong> 133
28+
<strong>Explanation:</strong> The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
29+
It can be shown that there are no ordered triplets of indices with a value greater than 133.
30+
</pre>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [1,2,3]
36+
<strong>Output:</strong> 0
37+
<strong>Explanation:</strong> The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
45+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
62+
```
63+
64+
### **C++**
65+
66+
```cpp
67+
68+
```
69+
70+
### **Go**
71+
72+
```go
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
```
81+
82+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2874. 有序三元组中的最大值 II](https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-ii)
2+
3+
[English Version](/solution/2800-2899/2874.Maximum%20Value%20of%20an%20Ordered%20Triplet%20II/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
10+
11+
<p>请你从所有满足&nbsp;<code>i &lt; j &lt; k</code> 的下标三元组 <code>(i, j, k)</code> 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 <code>0</code> 。</p>
12+
13+
<p><strong>下标三元组</strong> <code>(i, j, k)</code> 的值等于 <code>(nums[i] - nums[j]) * nums[k]</code> 。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>nums = [12,6,1,2,7]
21+
<strong>输出:</strong>77
22+
<strong>解释:</strong>下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。
23+
可以证明不存在值大于 77 的有序下标三元组。
24+
</pre>
25+
26+
<p><strong class="example">示例 2:</strong></p>
27+
28+
<pre>
29+
<strong>输入:</strong>nums = [1,10,3,4,19]
30+
<strong>输出:</strong>133
31+
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
32+
可以证明不存在值大于 133 的有序下标三元组。
33+
</pre>
34+
35+
<p><strong class="example">示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>nums = [1,2,3]
39+
<strong>输出:</strong>0
40+
<strong>解释:</strong>唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```java
71+
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
78+
```
79+
80+
### **Go**
81+
82+
```go
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [2874. Maximum Value of an Ordered Triplet II](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii)
2+
3+
[中文文档](/solution/2800-2899/2874.Maximum%20Value%20of%20an%20Ordered%20Triplet%20II/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
8+
9+
<p>Return <em><strong>the maximum value over all triplets of indices</strong></em> <code>(i, j, k)</code> <em>such that</em> <code>i &lt; j &lt; k</code><em>. </em>If all such triplets have a negative value, return <code>0</code>.</p>
10+
11+
<p>The <strong>value of a triplet of indices</strong> <code>(i, j, k)</code> is equal to <code>(nums[i] - nums[j]) * nums[k]</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [12,6,1,2,7]
18+
<strong>Output:</strong> 77
19+
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
20+
It can be shown that there are no ordered triplets of indices with a value greater than 77.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,10,3,4,19]
27+
<strong>Output:</strong> 133
28+
<strong>Explanation:</strong> The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
29+
It can be shown that there are no ordered triplets of indices with a value greater than 133.
30+
</pre>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [1,2,3]
36+
<strong>Output:</strong> 0
37+
<strong>Explanation:</strong> The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
62+
```
63+
64+
### **C++**
65+
66+
```cpp
67+
68+
```
69+
70+
### **Go**
71+
72+
```go
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
```
81+
82+
<!-- tabs:end -->

0 commit comments

Comments
 (0)