-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add weekly contest 381 and biweekly contest 122 (#2244)
Co-authored-by: Doocs Bot <[email protected]>
- Loading branch information
Showing
34 changed files
with
1,468 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...ion/3000-3099/3010.Divide an Array Into Subarrays With Minimum Cost I/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# [3010. 将数组分成最小总代价的子数组 I](https://leetcode.cn/problems/divide-an-array-into-subarrays-with-minimum-cost-i) | ||
|
||
[English Version](/solution/3000-3099/3010.Divide%20an%20Array%20Into%20Subarrays%20With%20Minimum%20Cost%20I/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> 。</p> | ||
|
||
<p>一个数组的 <strong>代价</strong> 是它的 <strong>第一个</strong> 元素。比方说,<code>[1,2,3]</code> 的代价是 <code>1</code> ,<code>[3,4,1]</code> 的代价是 <code>3</code> 。</p> | ||
|
||
<p>你需要将 <code>nums</code> 分成 <code>3</code> 个 <strong>连续且没有交集</strong> 的子数组。</p> | ||
|
||
<p>请你返回这些子数组的 <strong>最小</strong> 代价 <b>总和</b> 。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [1,2,3,12] | ||
<b>输出:</b>6 | ||
<b>解释:</b>最佳分割成 3 个子数组的方案是:[1] ,[2] 和 [3,12] ,总代价为 1 + 2 + 3 = 6 。 | ||
其他得到 3 个子数组的方案是: | ||
- [1] ,[2,3] 和 [12] ,总代价是 1 + 2 + 12 = 15 。 | ||
- [1,2] ,[3] 和 [12] ,总代价是 1 + 3 + 12 = 16 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [5,4,3] | ||
<b>输出:</b>12 | ||
<b>解释:</b>最佳分割成 3 个子数组的方案是:[5] ,[4] 和 [3] ,总代价为 5 + 4 + 3 = 12 。 | ||
12 是所有分割方案里的最小总代价。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [10,3,1,1] | ||
<b>输出:</b>12 | ||
<b>解释:</b>最佳分割成 3 个子数组的方案是:[10,3] ,[1] 和 [1] ,总代价为 10 + 1 + 1 = 12 。 | ||
12 是所有分割方案里的最小总代价。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>3 <= n <= 50</code></li> | ||
<li><code>1 <= nums[i] <= 50</code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
### 方法一 | ||
|
||
<!-- tabs:start --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
```java | ||
|
||
``` | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
```go | ||
|
||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
<!-- end --> |
77 changes: 77 additions & 0 deletions
77
.../3000-3099/3010.Divide an Array Into Subarrays With Minimum Cost I/README_EN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# [3010. Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i) | ||
|
||
[中文文档](/solution/3000-3099/3010.Divide%20an%20Array%20Into%20Subarrays%20With%20Minimum%20Cost%20I/README.md) | ||
|
||
## Description | ||
|
||
<p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p> | ||
|
||
<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> | ||
|
||
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p> | ||
|
||
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,3,12] | ||
<strong>Output:</strong> 6 | ||
<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. | ||
The other possible ways to form 3 subarrays are: | ||
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15. | ||
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [5,4,3] | ||
<strong>Output:</strong> 12 | ||
<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. | ||
It can be shown that 12 is the minimum cost achievable. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [10,3,1,1] | ||
<strong>Output:</strong> 12 | ||
<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. | ||
It can be shown that 12 is the minimum cost achievable. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>3 <= n <= 50</code></li> | ||
<li><code>1 <= nums[i] <= 50</code></li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
### Solution 1 | ||
|
||
<!-- tabs:start --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
```java | ||
|
||
``` | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
```go | ||
|
||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
<!-- end --> |
81 changes: 81 additions & 0 deletions
81
solution/3000-3099/3011.Find if Array Can Be Sorted/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# [3011. 判断一个数组是否可以变为有序](https://leetcode.cn/problems/find-if-array-can-be-sorted) | ||
|
||
[English Version](/solution/3000-3099/3011.Find%20if%20Array%20Can%20Be%20Sorted/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个下标从 <strong>0</strong> 开始且全是 <strong>正</strong> 整数的数组 <code>nums</code> 。</p> | ||
|
||
<p>一次 <b>操作</b> 中,如果两个 <strong>相邻</strong> 元素在二进制下数位为 <strong>1</strong> 的数目 <strong>相同</strong> ,那么你可以将这两个元素交换。你可以执行这个操作 <strong>任意次</strong> (<strong>也可以 0 次</strong>)。</p> | ||
|
||
<p>如果你可以使数组变有序,请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [8,4,2,30,15] | ||
<b>输出:</b>true | ||
<b>解释:</b>我们先观察每个元素的二进制表示。 2 ,4 和 8 分别都只有一个数位为 1 ,分别为 "10" ,"100" 和 "1000" 。15 和 30 分别有 4 个数位为 1 :"1111" 和 "11110" 。 | ||
我们可以通过 4 个操作使数组有序: | ||
- 交换 nums[0] 和 nums[1] 。8 和 4 分别只有 1 个数位为 1 。数组变为 [4,8,2,30,15] 。 | ||
- 交换 nums[1] 和 nums[2] 。8 和 2 分别只有 1 个数位为 1 。数组变为 [4,2,8,30,15] 。 | ||
- 交换 nums[0] 和 nums[1] 。4 和 2 分别只有 1 个数位为 1 。数组变为 [2,4,8,30,15] 。 | ||
- 交换 nums[3] 和 nums[4] 。30 和 15 分别有 4 个数位为 1 ,数组变为 [2,4,8,15,30] 。 | ||
数组变成有序的,所以我们返回 true 。 | ||
注意我们还可以通过其他的操作序列使数组变得有序。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [1,2,3,4,5] | ||
<b>输出:</b>true | ||
<b>解释:</b>数组已经是有序的,所以我们返回 true 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [3,16,8,4,2] | ||
<b>输出:</b>false | ||
<b>解释:</b>无法通过操作使数组变为有序。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 2<sup>8</sup></code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
### 方法一 | ||
|
||
<!-- tabs:start --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
```java | ||
|
||
``` | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
```go | ||
|
||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
<!-- end --> |
77 changes: 77 additions & 0 deletions
77
solution/3000-3099/3011.Find if Array Can Be Sorted/README_EN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# [3011. Find if Array Can Be Sorted](https://leetcode.com/problems/find-if-array-can-be-sorted) | ||
|
||
[中文文档](/solution/3000-3099/3011.Find%20if%20Array%20Can%20Be%20Sorted/README.md) | ||
|
||
## Description | ||
|
||
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p> | ||
|
||
<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> | ||
|
||
<p>Return <code>true</code> <em>if you can sort the array, else return </em><code>false</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [8,4,2,30,15] | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110". | ||
We can sort the array using 4 operations: | ||
- 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]. | ||
- 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]. | ||
- 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]. | ||
- 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]. | ||
The array has become sorted, hence we return true. | ||
Note that there may be other sequences of operations which also sort the array. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,3,4,5] | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> The array is already sorted, hence we return true. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [3,16,8,4,2] | ||
<strong>Output:</strong> false | ||
<strong>Explanation:</strong> It can be shown that it is not possible to sort the input array using any number of operations. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 2<sup>8</sup></code></li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
### Solution 1 | ||
|
||
<!-- tabs:start --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
```java | ||
|
||
``` | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
```go | ||
|
||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
<!-- end --> |
Oops, something went wrong.