Skip to content

Commit 2908f60

Browse files
committed
chore: add new lc problems
1 parent 45ce2d1 commit 2908f60

File tree

8 files changed

+341
-30
lines changed

8 files changed

+341
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [2339. All the Matches of the League](https://leetcode.cn/problems/all-the-matches-of-the-league)
2+
3+
[English Version](/solution/2300-2399/2339.All%20the%20Matches%20of%20the%20League/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Teams</code></p>
10+
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| team_name | varchar |
16+
+-------------+---------+
17+
team_name is the primary key of this table.
18+
Each row of this table shows the name of a team.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p>Write an SQL query that reports all the possible matches of the league. Note that every two teams play two matches: home and away.&nbsp;</p>
24+
25+
<p>Return the result table in <strong>any order</strong>.</p>
26+
27+
<p>The query result format is in the following example.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Example 1:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong>
34+
Teams table:
35+
+-------------+
36+
| team_name |
37+
+-------------+
38+
| Leetcode FC |
39+
| Ahly SC |
40+
| Real Madrid |
41+
+-------------+
42+
<strong>Output:</strong>
43+
+-------------+-------------+
44+
| home_team | away_team |
45+
+-------------+-------------+
46+
| Real Madrid | Leetcode FC |
47+
| Real Madrid | Ahly SC |
48+
| Leetcode FC | Real Madrid |
49+
| Leetcode FC | Ahly SC |
50+
| Ahly SC | Real Madrid |
51+
| Ahly SC | Leetcode FC |
52+
+-------------+-------------+
53+
<strong>Explanation:</strong> All the matches of the league are shown in the table.
54+
</pre>
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
<!-- tabs:start -->
61+
62+
### **SQL**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```sql
67+
68+
```
69+
70+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [2339. All the Matches of the League](https://leetcode.com/problems/all-the-matches-of-the-league)
2+
3+
[中文文档](/solution/2300-2399/2339.All%20the%20Matches%20of%20the%20League/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Teams</code></p>
8+
9+
<pre>
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| team_name | varchar |
14+
+-------------+---------+
15+
team_name is the primary key of this table.
16+
Each row of this table shows the name of a team.
17+
</pre>
18+
19+
<p>&nbsp;</p>
20+
21+
<p>Write an SQL query that reports all the possible matches of the league. Note that every two teams play two matches: home and away.&nbsp;</p>
22+
23+
<p>Return the result table in <strong>any order</strong>.</p>
24+
25+
<p>The query result format is in the following example.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong>
32+
Teams table:
33+
+-------------+
34+
| team_name |
35+
+-------------+
36+
| Leetcode FC |
37+
| Ahly SC |
38+
| Real Madrid |
39+
+-------------+
40+
<strong>Output:</strong>
41+
+-------------+-------------+
42+
| home_team | away_team |
43+
+-------------+-------------+
44+
| Real Madrid | Leetcode FC |
45+
| Real Madrid | Ahly SC |
46+
| Leetcode FC | Real Madrid |
47+
| Leetcode FC | Ahly SC |
48+
| Ahly SC | Real Madrid |
49+
| Ahly SC | Leetcode FC |
50+
+-------------+-------------+
51+
<strong>Explanation:</strong> All the matches of the league are shown in the table.
52+
</pre>
53+
54+
55+
## Solutions
56+
57+
<!-- tabs:start -->
58+
59+
### **SQL**
60+
61+
```sql
62+
63+
```
64+
65+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [2340. Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.cn/problems/minimum-adjacent-swaps-to-make-a-valid-array)
2+
3+
[English Version](/solution/2300-2399/2340.Minimum%20Adjacent%20Swaps%20to%20Make%20a%20Valid%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
10+
11+
<p><strong>Swaps</strong> of <strong>adjacent</strong> elements are able to be performed on <code>nums</code>.</p>
12+
13+
<p>A <strong>valid</strong> array meets the following conditions:</p>
14+
15+
<ul>
16+
<li>The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array.</li>
17+
<li>The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array.</li>
18+
</ul>
19+
20+
<p>Return <em>the <strong>minimum</strong> swaps required to make </em><code>nums</code><em> a valid array</em>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [3,4,5,5,3,1]
27+
<strong>Output:</strong> 6
28+
<strong>Explanation:</strong> Perform the following swaps:
29+
- Swap 1: Swap the 3<sup>rd</sup> and 4<sup>th</sup> elements, nums is then [3,4,5,<u><strong>3</strong></u>,<u><strong>5</strong></u>,1].
30+
- Swap 2: Swap the 4<sup>th</sup> and 5<sup>th</sup> elements, nums is then [3,4,5,3,<u><strong>1</strong></u>,<u><strong>5</strong></u>].
31+
- Swap 3: Swap the 3<sup>rd</sup> and 4<sup>th</sup> elements, nums is then [3,4,5,<u><strong>1</strong></u>,<u><strong>3</strong></u>,5].
32+
- Swap 4: Swap the 2<sup>nd</sup> and 3<sup>rd</sup> elements, nums is then [3,4,<u><strong>1</strong></u>,<u><strong>5</strong></u>,3,5].
33+
- Swap 5: Swap the 1<sup>st</sup> and 2<sup>nd</sup> elements, nums is then [3,<u><strong>1</strong></u>,<u><strong>4</strong></u>,5,3,5].
34+
- Swap 6: Swap the 0<sup>th</sup> and 1<sup>st</sup> elements, nums is then [<u><strong>1</strong></u>,<u><strong>3</strong></u>,4,5,3,5].
35+
It can be shown that 6 swaps is the minimum swaps required to make a valid array.
36+
</pre>
37+
38+
<strong>Example 2:</strong>
39+
40+
<pre>
41+
<strong>Input:</strong> nums = [9]
42+
<strong>Output:</strong> 0
43+
<strong>Explanation:</strong> The array is already valid, so we return 0.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
52+
</ul>
53+
54+
## 解法
55+
56+
<!-- 这里可写通用的实现逻辑 -->
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
66+
```
67+
68+
### **Java**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```java
73+
74+
```
75+
76+
### **TypeScript**
77+
78+
```ts
79+
80+
```
81+
82+
### **...**
83+
84+
```
85+
86+
```
87+
88+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [2340. Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array)
2+
3+
[中文文档](/solution/2300-2399/2340.Minimum%20Adjacent%20Swaps%20to%20Make%20a%20Valid%20Array/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><strong>Swaps</strong> of <strong>adjacent</strong> elements are able to be performed on <code>nums</code>.</p>
10+
11+
<p>A <strong>valid</strong> array meets the following conditions:</p>
12+
13+
<ul>
14+
<li>The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array.</li>
15+
<li>The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array.</li>
16+
</ul>
17+
18+
<p>Return <em>the <strong>minimum</strong> swaps required to make </em><code>nums</code><em> a valid array</em>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> nums = [3,4,5,5,3,1]
25+
<strong>Output:</strong> 6
26+
<strong>Explanation:</strong> Perform the following swaps:
27+
- Swap 1: Swap the 3<sup>rd</sup> and 4<sup>th</sup> elements, nums is then [3,4,5,<u><strong>3</strong></u>,<u><strong>5</strong></u>,1].
28+
- Swap 2: Swap the 4<sup>th</sup> and 5<sup>th</sup> elements, nums is then [3,4,5,3,<u><strong>1</strong></u>,<u><strong>5</strong></u>].
29+
- Swap 3: Swap the 3<sup>rd</sup> and 4<sup>th</sup> elements, nums is then [3,4,5,<u><strong>1</strong></u>,<u><strong>3</strong></u>,5].
30+
- Swap 4: Swap the 2<sup>nd</sup> and 3<sup>rd</sup> elements, nums is then [3,4,<u><strong>1</strong></u>,<u><strong>5</strong></u>,3,5].
31+
- Swap 5: Swap the 1<sup>st</sup> and 2<sup>nd</sup> elements, nums is then [3,<u><strong>1</strong></u>,<u><strong>4</strong></u>,5,3,5].
32+
- Swap 6: Swap the 0<sup>th</sup> and 1<sup>st</sup> elements, nums is then [<u><strong>1</strong></u>,<u><strong>3</strong></u>,4,5,3,5].
33+
It can be shown that 6 swaps is the minimum swaps required to make a valid array.
34+
</pre>
35+
36+
<strong>Example 2:</strong>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [9]
40+
<strong>Output:</strong> 0
41+
<strong>Explanation:</strong> The array is already valid, so we return 0.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
50+
</ul>
51+
52+
## Solutions
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
```java
65+
66+
```
67+
68+
### **TypeScript**
69+
70+
```ts
71+
72+
```
73+
74+
### **...**
75+
76+
```
77+
78+
```
79+
80+
<!-- tabs:end -->

0 commit comments

Comments
 (0)