Skip to content

Commit d15457c

Browse files
yanglbmeidoocs
andauthored
feat: add weekly contest 381 and biweekly contest 122 (doocs#2244)
Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
1 parent 41aa731 commit d15457c

File tree

34 files changed

+1468
-0
lines changed

34 files changed

+1468
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [3010. 将数组分成最小总代价的子数组 I](https://leetcode.cn/problems/divide-an-array-into-subarrays-with-minimum-cost-i)
2+
3+
[English Version](/solution/3000-3099/3010.Divide%20an%20Array%20Into%20Subarrays%20With%20Minimum%20Cost%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;。</p>
10+
11+
<p>一个数组的 <strong>代价</strong>&nbsp;是它的 <strong>第一个</strong>&nbsp;元素。比方说,<code>[1,2,3]</code>&nbsp;的代价是&nbsp;<code>1</code>&nbsp;,<code>[3,4,1]</code>&nbsp;的代价是&nbsp;<code>3</code>&nbsp;。</p>
12+
13+
<p>你需要将&nbsp;<code>nums</code>&nbsp;分成&nbsp;<code>3</code>&nbsp;&nbsp;<strong>连续且没有交集</strong>&nbsp;的子数组。</p>
14+
15+
<p>请你返回这些子数组的 <strong>最小</strong>&nbsp;代价&nbsp;<b>总和</b>&nbsp;。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<b>输入:</b>nums = [1,2,3,12]
23+
<b>输出:</b>6
24+
<b>解释:</b>最佳分割成 3 个子数组的方案是:[1] ,[2] 和 [3,12] ,总代价为 1 + 2 + 3 = 6 。
25+
其他得到 3 个子数组的方案是:
26+
- [1] ,[2,3] 和 [12] ,总代价是 1 + 2 + 12 = 15 。
27+
- [1,2] ,[3] 和 [12] ,总代价是 1 + 3 + 12 = 16 。
28+
</pre>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<pre>
33+
<b>输入:</b>nums = [5,4,3]
34+
<b>输出:</b>12
35+
<b>解释:</b>最佳分割成 3 个子数组的方案是:[5] ,[4] 和 [3] ,总代价为 5 + 4 + 3 = 12 。
36+
12 是所有分割方案里的最小总代价。
37+
</pre>
38+
39+
<p><strong class="example">示例 3:</strong></p>
40+
41+
<pre>
42+
<b>输入:</b>nums = [10,3,1,1]
43+
<b>输出:</b>12
44+
<b>解释:</b>最佳分割成 3 个子数组的方案是:[10,3] ,[1] 和 [1] ,总代价为 10 + 1 + 1 = 12 。
45+
12 是所有分割方案里的最小总代价。
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>提示:</strong></p>
51+
52+
<ul>
53+
<li><code>3 &lt;= n &lt;= 50</code></li>
54+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
55+
</ul>
56+
57+
## 解法
58+
59+
### 方法一
60+
61+
<!-- tabs:start -->
62+
63+
```python
64+
65+
```
66+
67+
```java
68+
69+
```
70+
71+
```cpp
72+
73+
```
74+
75+
```go
76+
77+
```
78+
79+
<!-- tabs:end -->
80+
81+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [3010. Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i)
2+
3+
[中文文档](/solution/3000-3099/3010.Divide%20an%20Array%20Into%20Subarrays%20With%20Minimum%20Cost%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
8+
9+
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
10+
11+
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
12+
13+
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [1,2,3,12]
20+
<strong>Output:</strong> 6
21+
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
22+
The other possible ways to form 3 subarrays are:
23+
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
24+
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [5,4,3]
31+
<strong>Output:</strong> 12
32+
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
33+
It can be shown that 12 is the minimum cost achievable.
34+
</pre>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [10,3,1,1]
40+
<strong>Output:</strong> 12
41+
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
42+
It can be shown that 12 is the minimum cost achievable.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>3 &lt;= n &lt;= 50</code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
51+
</ul>
52+
53+
## Solutions
54+
55+
### Solution 1
56+
57+
<!-- tabs:start -->
58+
59+
```python
60+
61+
```
62+
63+
```java
64+
65+
```
66+
67+
```cpp
68+
69+
```
70+
71+
```go
72+
73+
```
74+
75+
<!-- tabs:end -->
76+
77+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [3011. 判断一个数组是否可以变为有序](https://leetcode.cn/problems/find-if-array-can-be-sorted)
2+
3+
[English Version](/solution/3000-3099/3011.Find%20if%20Array%20Can%20Be%20Sorted/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始且全是 <strong>正</strong>&nbsp;整数的数组&nbsp;<code>nums</code>&nbsp;。</p>
10+
11+
<p>一次 <b>操作</b>&nbsp;中,如果两个 <strong>相邻</strong>&nbsp;元素在二进制下数位为 <strong>1</strong>&nbsp;的数目 <strong>相同</strong>&nbsp;,那么你可以将这两个元素交换。你可以执行这个操作 <strong>任意次</strong>&nbsp;(<strong>也可以 0 次</strong>)。</p>
12+
13+
<p>如果你可以使数组变有序,请你返回&nbsp;<code>true</code> ,否则返回&nbsp;<code>false</code>&nbsp;。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<b>输入:</b>nums = [8,4,2,30,15]
21+
<b>输出:</b>true
22+
<b>解释:</b>我们先观察每个元素的二进制表示。 2 ,4 和 8 分别都只有一个数位为 1 ,分别为 "10" ,"100" 和 "1000" 。15 和 30 分别有 4 个数位为 1 :"1111" 和 "11110" 。
23+
我们可以通过 4 个操作使数组有序:
24+
- 交换 nums[0] 和 nums[1] 。8 和 4 分别只有 1 个数位为 1 。数组变为 [4,8,2,30,15] 。
25+
- 交换 nums[1] 和 nums[2] 。8 和 2 分别只有 1 个数位为 1 。数组变为 [4,2,8,30,15] 。
26+
- 交换 nums[0] 和 nums[1] 。4 和 2 分别只有 1 个数位为 1 。数组变为 [2,4,8,30,15] 。
27+
- 交换 nums[3] 和 nums[4] 。30 和 15 分别有 4 个数位为 1 ,数组变为 [2,4,8,15,30] 。
28+
数组变成有序的,所以我们返回 true 。
29+
注意我们还可以通过其他的操作序列使数组变得有序。
30+
</pre>
31+
32+
<p><strong class="example">示例 2:</strong></p>
33+
34+
<pre>
35+
<b>输入:</b>nums = [1,2,3,4,5]
36+
<b>输出:</b>true
37+
<b>解释:</b>数组已经是有序的,所以我们返回 true 。
38+
</pre>
39+
40+
<p><strong class="example">示例 3:</strong></p>
41+
42+
<pre>
43+
<b>输入:</b>nums = [3,16,8,4,2]
44+
<b>输出:</b>false
45+
<b>解释:</b>无法通过操作使数组变为有序。
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>提示:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
54+
<li><code>1 &lt;= nums[i] &lt;= 2<sup>8</sup></code></li>
55+
</ul>
56+
57+
## 解法
58+
59+
### 方法一
60+
61+
<!-- tabs:start -->
62+
63+
```python
64+
65+
```
66+
67+
```java
68+
69+
```
70+
71+
```cpp
72+
73+
```
74+
75+
```go
76+
77+
```
78+
79+
<!-- tabs:end -->
80+
81+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [3011. Find if Array Can Be Sorted](https://leetcode.com/problems/find-if-array-can-be-sorted)
2+
3+
[中文文档](/solution/3000-3099/3011.Find%20if%20Array%20Can%20Be%20Sorted/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
8+
9+
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed to do this operation <strong>any</strong> number of times (<strong>including zero</strong>).</p>
10+
11+
<p>Return <code>true</code> <em>if you can sort the array, else return </em><code>false</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [8,4,2,30,15]
18+
<strong>Output:</strong> true
19+
<strong>Explanation:</strong> Let&#39;s look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation &quot;10&quot;, &quot;100&quot;, and &quot;1000&quot; respectively. The numbers 15 and 30 have four set bits each with binary representation &quot;1111&quot; and &quot;11110&quot;.
20+
We can sort the array using 4 operations:
21+
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
22+
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
23+
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
24+
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
25+
The array has become sorted, hence we return true.
26+
Note that there may be other sequences of operations which also sort the array.
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [1,2,3,4,5]
33+
<strong>Output:</strong> true
34+
<strong>Explanation:</strong> The array is already sorted, hence we return true.
35+
</pre>
36+
37+
<p><strong class="example">Example 3:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> nums = [3,16,8,4,2]
41+
<strong>Output:</strong> false
42+
<strong>Explanation:</strong> It can be shown that it is not possible to sort the input array using any number of operations.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= 2<sup>8</sup></code></li>
51+
</ul>
52+
53+
## Solutions
54+
55+
### Solution 1
56+
57+
<!-- tabs:start -->
58+
59+
```python
60+
61+
```
62+
63+
```java
64+
65+
```
66+
67+
```cpp
68+
69+
```
70+
71+
```go
72+
73+
```
74+
75+
<!-- tabs:end -->
76+
77+
<!-- end -->

0 commit comments

Comments
 (0)