Skip to content

Commit 763b141

Browse files
authored
feat: add weekly contest 368 (#1862)
1 parent 2e5040d commit 763b141

File tree

19 files changed

+853
-57
lines changed

19 files changed

+853
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2908. 元素和最小的山形三元组 I](https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i)
2+
3+
[English Version](/solution/2900-2999/2908.Minimum%20Sum%20of%20Mountain%20Triplets%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
10+
11+
<p>如果下标三元组 <code>(i, j, k)</code> 满足下述全部条件,则认为它是一个 <strong>山形三元组</strong> :</p>
12+
13+
<ul>
14+
<li><code>i &lt; j &lt; k</code></li>
15+
<li><code>nums[i] &lt; nums[j]</code> 且 <code>nums[k] &lt; nums[j]</code></li>
16+
</ul>
17+
18+
<p>请你找出 <code>nums</code> 中 <strong>元素和最小</strong> 的山形三元组,并返回其 <strong>元素和</strong> 。如果不存在满足条件的三元组,返回 <code>-1</code> 。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong class="example">示例 1:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong>nums = [8,6,1,5,3]
26+
<strong>输出:</strong>9
27+
<strong>解释:</strong>三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为:
28+
- 2 &lt; 3 &lt; 4
29+
- nums[2] &lt; nums[3] 且 nums[4] &lt; nums[3]
30+
这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。
31+
</pre>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>nums = [5,4,8,7,10,2]
37+
<strong>输出:</strong>13
38+
<strong>解释:</strong>三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为:
39+
- 1 &lt; 3 &lt; 5
40+
- nums[1] &lt; nums[3] 且 nums[5] &lt; nums[3]
41+
这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。
42+
</pre>
43+
44+
<p><strong class="example">示例 3:</strong></p>
45+
46+
<pre>
47+
<strong>输入:</strong>nums = [6,5,4,3,4,5]
48+
<strong>输出:</strong>-1
49+
<strong>解释:</strong>可以证明 nums 中不存在山形三元组。
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>3 &lt;= nums.length &lt;= 50</code></li>
58+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
59+
</ul>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
73+
```
74+
75+
### **Java**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [2908. Minimum Sum of Mountain Triplets I](https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i)
2+
3+
[中文文档](/solution/2900-2999/2908.Minimum%20Sum%20of%20Mountain%20Triplets%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>
8+
9+
<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>
10+
11+
<ul>
12+
<li><code>i &lt; j &lt; k</code></li>
13+
<li><code>nums[i] &lt; nums[j]</code> and <code>nums[k] &lt; nums[j]</code></li>
14+
</ul>
15+
16+
<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [8,6,1,5,3]
23+
<strong>Output:</strong> 9
24+
<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
25+
- 2 &lt; 3 &lt; 4
26+
- nums[2] &lt; nums[3] and nums[4] &lt; nums[3]
27+
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> nums = [5,4,8,7,10,2]
34+
<strong>Output:</strong> 13
35+
<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
36+
- 1 &lt; 3 &lt; 5
37+
- nums[1] &lt; nums[3] and nums[5] &lt; nums[3]
38+
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> nums = [6,5,4,3,4,5]
45+
<strong>Output:</strong> -1
46+
<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>3 &lt;= nums.length &lt;= 50</code></li>
54+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2909. 元素和最小的山形三元组 II](https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-ii)
2+
3+
[English Version](/solution/2900-2999/2909.Minimum%20Sum%20of%20Mountain%20Triplets%20II/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
10+
11+
<p>如果下标三元组 <code>(i, j, k)</code> 满足下述全部条件,则认为它是一个 <strong>山形三元组</strong> :</p>
12+
13+
<ul>
14+
<li><code>i &lt; j &lt; k</code></li>
15+
<li><code>nums[i] &lt; nums[j]</code> 且 <code>nums[k] &lt; nums[j]</code></li>
16+
</ul>
17+
18+
<p>请你找出 <code>nums</code> 中 <strong>元素和最小</strong> 的山形三元组,并返回其 <strong>元素和</strong> 。如果不存在满足条件的三元组,返回 <code>-1</code> 。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong class="example">示例 1:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong>nums = [8,6,1,5,3]
26+
<strong>输出:</strong>9
27+
<strong>解释:</strong>三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为:
28+
- 2 &lt; 3 &lt; 4
29+
- nums[2] &lt; nums[3] 且 nums[4] &lt; nums[3]
30+
这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。
31+
</pre>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>nums = [5,4,8,7,10,2]
37+
<strong>输出:</strong>13
38+
<strong>解释:</strong>三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为:
39+
- 1 &lt; 3 &lt; 5
40+
- nums[1] &lt; nums[3] 且 nums[5] &lt; nums[3]
41+
这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。
42+
</pre>
43+
44+
<p><strong class="example">示例 3:</strong></p>
45+
46+
<pre>
47+
<strong>输入:</strong>nums = [6,5,4,3,4,5]
48+
<strong>输出:</strong>-1
49+
<strong>解释:</strong>可以证明 nums 中不存在山形三元组。
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
58+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>8</sup></code></li>
59+
</ul>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
73+
```
74+
75+
### **Java**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [2909. Minimum Sum of Mountain Triplets II](https://leetcode.com/problems/minimum-sum-of-mountain-triplets-ii)
2+
3+
[中文文档](/solution/2900-2999/2909.Minimum%20Sum%20of%20Mountain%20Triplets%20II/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>
8+
9+
<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>
10+
11+
<ul>
12+
<li><code>i &lt; j &lt; k</code></li>
13+
<li><code>nums[i] &lt; nums[j]</code> and <code>nums[k] &lt; nums[j]</code></li>
14+
</ul>
15+
16+
<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [8,6,1,5,3]
23+
<strong>Output:</strong> 9
24+
<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
25+
- 2 &lt; 3 &lt; 4
26+
- nums[2] &lt; nums[3] and nums[4] &lt; nums[3]
27+
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> nums = [5,4,8,7,10,2]
34+
<strong>Output:</strong> 13
35+
<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
36+
- 1 &lt; 3 &lt; 5
37+
- nums[1] &lt; nums[3] and nums[5] &lt; nums[3]
38+
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> nums = [6,5,4,3,4,5]
45+
<strong>Output:</strong> -1
46+
<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
54+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>8</sup></code></li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
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 -->

0 commit comments

Comments
 (0)