Skip to content

Commit d907a17

Browse files
committed
feat: add biweekly contest 159
1 parent 76cb00e commit d907a17

File tree

15 files changed

+1039
-1
lines changed

15 files changed

+1039
-1
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3587.Minimum%20Adjacent%20Swaps%20to%20Alternate%20Parity/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3587. 最小相邻交换至奇偶交替](https://leetcode.cn/problems/minimum-adjacent-swaps-to-alternate-parity)
10+
11+
[English Version](/solution/3500-3599/3587.Minimum%20Adjacent%20Swaps%20to%20Alternate%20Parity/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个由互不相同的整数组成的数组 <code>nums</code>&nbsp;。</p>
18+
19+
<p>在一次操作中,你可以交换任意两个&nbsp;<strong>相邻&nbsp;</strong>元素。</p>
20+
21+
<p>在一个排列中,当所有相邻元素的奇偶性交替出现,我们认为该排列是 <strong>有效排列</strong>。这意味着每对相邻元素中一个是偶数,一个是奇数。</p>
22+
23+
<p>请返回将 <code>nums</code> 变成任意一种&nbsp;<strong>有效排列</strong>&nbsp;所需的最小相邻交换次数。</p>
24+
25+
<p>如果无法重排 <code>nums</code> 来获得有效排列,则返回 <code>-1</code>。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong class="example">示例 1:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>输入:</strong> <span class="example-io">nums = [2,4,6,5,7]</span></p>
33+
34+
<p><span class="example-io"><b>输出:</b>3</span></p>
35+
36+
<p><strong>解释:</strong></p>
37+
38+
<p>将 5 和 6 交换,数组变成&nbsp; <code>[2,4,5,6,7]</code></p>
39+
40+
<p>将 5 和 4&nbsp;交换,数组变成&nbsp; <code>[2,5,4,6,7]</code></p>
41+
42+
<p>将 6&nbsp;和 7&nbsp;交换,数组变成&nbsp;&nbsp;<code>[2,5,4,7,6]</code>。此时是一个有效排列。因此答案是 3。</p>
43+
</div>
44+
45+
<p><strong class="example">示例 2:</strong></p>
46+
47+
<div class="example-block">
48+
<p><strong>输入:</strong> <span class="example-io">nums = [2,4,5,7]</span></p>
49+
50+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
51+
52+
<p><strong>解释:</strong></p>
53+
54+
<p>将 4&nbsp;和 5&nbsp;交换,数组变成 <code>[2,5,4,7]</code>。此时是一个有效排列。因此答案是 1。</p>
55+
</div>
56+
57+
<p><strong class="example">示例 3:</strong></p>
58+
59+
<div class="example-block">
60+
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3]</span></p>
61+
62+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
63+
64+
<p><strong>解释:</strong></p>
65+
66+
<p>数组已经是有效排列,因此不需要任何操作。</p>
67+
</div>
68+
69+
<p><strong class="example">示例 4:</strong></p>
70+
71+
<div class="example-block">
72+
<p><b>输入:</b>&nbsp;<span class="example-io">nums = [4,5,6,8]</span></p>
73+
74+
<p><span class="example-io"><b>输出:</b>-1</span></p>
75+
76+
<p><b>解释:</b></p>
77+
78+
<p>没有任何一种排列可以满足奇偶交替的要求,因此返回 -1。</p>
79+
</div>
80+
81+
<p>&nbsp;</p>
82+
83+
<p><strong>提示:</strong></p>
84+
85+
<ul>
86+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
87+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
88+
<li><code>nums</code>&nbsp;中的所有元素都是 <strong>唯一</strong> 的</li>
89+
</ul>
90+
91+
<!-- description:end -->
92+
93+
## 解法
94+
95+
<!-- solution:start -->
96+
97+
### 方法一
98+
99+
<!-- tabs:start -->
100+
101+
#### Python3
102+
103+
```python
104+
105+
```
106+
107+
#### Java
108+
109+
```java
110+
111+
```
112+
113+
#### C++
114+
115+
```cpp
116+
117+
```
118+
119+
#### Go
120+
121+
```go
122+
123+
```
124+
125+
<!-- tabs:end -->
126+
127+
<!-- solution:end -->
128+
129+
<!-- problem:end -->
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3587.Minimum%20Adjacent%20Swaps%20to%20Alternate%20Parity/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3587. Minimum Adjacent Swaps to Alternate Parity](https://leetcode.com/problems/minimum-adjacent-swaps-to-alternate-parity)
10+
11+
[中文文档](/solution/3500-3599/3587.Minimum%20Adjacent%20Swaps%20to%20Alternate%20Parity/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an array <code>nums</code> of <strong>distinct</strong> integers.</p>
18+
19+
<p>In one operation, you can swap any two <strong>adjacent</strong> elements in the array.</p>
20+
21+
<p>An arrangement of the array is considered <strong>valid</strong> if the parity of adjacent elements <strong>alternates</strong>, meaning every pair of neighboring elements consists of one even and one odd number.</p>
22+
23+
<p>Return the <strong>minimum</strong> number of adjacent swaps required to transform <code>nums</code> into any valid arrangement.</p>
24+
25+
<p>If it is impossible to rearrange <code>nums</code> such that no two adjacent elements have the same parity, return <code>-1</code>.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,6,5,7]</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
34+
35+
<p><strong>Explanation:</strong></p>
36+
37+
<p>Swapping 5 and 6, the array becomes <code>[2,4,5,6,7]</code></p>
38+
39+
<p>Swapping 5 and 4, the array becomes <code>[2,5,4,6,7]</code></p>
40+
41+
<p>Swapping 6 and 7, the array becomes <code>[2,5,4,7,6]</code>. The array is now a valid arrangement. Thus, the answer is 3.</p>
42+
</div>
43+
44+
<p><strong class="example">Example 2:</strong></p>
45+
46+
<div class="example-block">
47+
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,5,7]</span></p>
48+
49+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
50+
51+
<p><strong>Explanation:</strong></p>
52+
53+
<p>By swapping 4 and 5, the array becomes <code>[2,5,4,7]</code>, which is a valid arrangement. Thus, the answer is 1.</p>
54+
</div>
55+
56+
<p><strong class="example">Example 3:</strong></p>
57+
58+
<div class="example-block">
59+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
60+
61+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
62+
63+
<p><strong>Explanation:</strong></p>
64+
65+
<p>The array is already a valid arrangement. Thus, no operations are needed.</p>
66+
</div>
67+
68+
<p><strong class="example">Example 4:</strong></p>
69+
70+
<div class="example-block">
71+
<p><strong>Input:</strong> <span class="example-io">nums = [4,5,6,8]</span></p>
72+
73+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
74+
75+
<p><strong>Explanation:</strong></p>
76+
77+
<p>No valid arrangement is possible. Thus, the answer is -1.</p>
78+
</div>
79+
80+
<p>&nbsp;</p>
81+
<p><strong>Constraints:</strong></p>
82+
83+
<ul>
84+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
85+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
86+
<li>All elements in <code>nums</code> are <strong>distinct</strong>.</li>
87+
</ul>
88+
89+
<!-- description:end -->
90+
91+
## Solutions
92+
93+
<!-- solution:start -->
94+
95+
### Solution 1
96+
97+
<!-- tabs:start -->
98+
99+
#### Python3
100+
101+
```python
102+
103+
```
104+
105+
#### Java
106+
107+
```java
108+
109+
```
110+
111+
#### C++
112+
113+
```cpp
114+
115+
```
116+
117+
#### Go
118+
119+
```go
120+
121+
```
122+
123+
<!-- tabs:end -->
124+
125+
<!-- solution:end -->
126+
127+
<!-- problem:end -->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3588.Find%20Maximum%20Area%20of%20a%20Triangle/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3588. 找到最大三角形面积](https://leetcode.cn/problems/find-maximum-area-of-a-triangle)
10+
11+
[English Version](/solution/3500-3599/3588.Find%20Maximum%20Area%20of%20a%20Triangle/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个二维数组 <code>coords</code>,大小为 <code>n x 2</code>,表示一个无限笛卡尔平面上 <code>n</code> 个点的坐标。</p>
18+
19+
<p>找出一个 <strong>最大</strong>&nbsp;三角形的 <strong>两倍&nbsp;</strong>面积,其中三角形的三个顶点来自 <code>coords</code> 中的任意三个点,并且该三角形至少有一条边与 x 轴或 y 轴平行。严格地说,如果该三角形的最大面积为 <code>A</code>,则返回 <code>2 * A</code>。</p>
20+
21+
<p>如果不存在这样的三角形,返回 -1。</p>
22+
23+
<p><strong>注意</strong>,三角形的面积 <strong>不能</strong> 为零。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong> <span class="example-io">coords = [[1,1],[1,2],[3,2],[3,3]]</span></p>
31+
32+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3500-3599/3588.Find%20Maximum%20Area%20of%20a%20Triangle/images/image-20250420010047-1.png" style="width: 300px; height: 289px;" /></p>
37+
38+
<p>图中的三角形的底边为 1,高为 2。因此,它的面积为 <code>1/2 * 底边 * 高 = 1</code>。</p>
39+
</div>
40+
41+
<p><strong class="example">示例 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><strong>输入:</strong> <span class="example-io">coords = [[1,1],[2,2],[3,3]]</span></p>
45+
46+
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
47+
48+
<p><strong>解释:</strong></p>
49+
50+
<p>唯一可能的三角形的顶点是 <code>(1, 1)</code>、<code>(2, 2)</code> 和 <code>(3, 3)</code>。它的任意边都不与 x 轴或 y 轴平行。</p>
51+
</div>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= n == coords.length &lt;= 10<sup>5</sup></code></li>
59+
<li><code>1 &lt;= coords[i][0], coords[i][1] &lt;= 10<sup>6</sup></code></li>
60+
<li>所有 <code>coords[i]</code> 都是 <strong>唯一</strong> 的。</li>
61+
</ul>
62+
63+
<!-- description:end -->
64+
65+
## 解法
66+
67+
<!-- solution:start -->
68+
69+
### 方法一
70+
71+
<!-- tabs:start -->
72+
73+
#### Python3
74+
75+
```python
76+
77+
```
78+
79+
#### Java
80+
81+
```java
82+
83+
```
84+
85+
#### C++
86+
87+
```cpp
88+
89+
```
90+
91+
#### Go
92+
93+
```go
94+
95+
```
96+
97+
<!-- tabs:end -->
98+
99+
<!-- solution:end -->
100+
101+
<!-- problem:end -->

0 commit comments

Comments
 (0)