-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add biweekly contest 116 and weekly contest 369 (#1898)
* feat: add biweekly contest 116 and weekly contest 369 * chore: optimised images with calibre/image-actions --------- Co-authored-by: Doocs Bot <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,565 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares 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,101 @@ | ||
# [2913. 子数组不同元素数目的平方和 I](https://leetcode.cn/problems/subarrays-distinct-element-sum-of-squares-i) | ||
|
||
[English Version](/solution/2900-2999/2913.Subarrays%20Distinct%20Element%20Sum%20of%20Squares%20I/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p> | ||
|
||
<p>定义 <code>nums</code> 一个子数组的 <strong>不同计数</strong> 值如下:</p> | ||
|
||
<ul> | ||
<li>令 <code>nums[i..j]</code> 表示 <code>nums</code> 中所有下标在 <code>i</code> 到 <code>j</code> 范围内的元素构成的子数组(满足 <code>0 <= i <= j < nums.length</code> ),那么我们称子数组 <code>nums[i..j]</code> 中不同值的数目为 <code>nums[i..j]</code> 的不同计数。</li> | ||
</ul> | ||
|
||
<p>请你返回 <code>nums</code> 中所有子数组的 <strong>不同计数</strong> 的 <strong>平方</strong> 和。</p> | ||
|
||
<p>由于答案可能会很大,请你将它对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p> | ||
|
||
<p>子数组指的是一个数组里面一段连续 <strong>非空</strong> 的元素序列。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>示例 1:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [1,2,1] | ||
<b>输出:</b>15 | ||
<b>解释:</b>六个子数组分别为: | ||
[1]: 1 个互不相同的元素。 | ||
[2]: 1 个互不相同的元素。 | ||
[1]: 1 个互不相同的元素。 | ||
[1,2]: 2 个互不相同的元素。 | ||
[2,1]: 2 个互不相同的元素。 | ||
[1,2,1]: 2 个互不相同的元素。 | ||
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15 。 | ||
</pre> | ||
|
||
<p><strong>示例 2:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [2,2] | ||
<b>输出:3</b> | ||
<strong>解释:</strong>三个子数组分别为: | ||
[2]: 1 个互不相同的元素。 | ||
[2]: 1 个互不相同的元素。 | ||
[2,2]: 1 个互不相同的元素。 | ||
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3 。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
88 changes: 88 additions & 0 deletions
88
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares 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,88 @@ | ||
# [2913. Subarrays Distinct Element Sum of Squares I](https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i) | ||
|
||
[中文文档](/solution/2900-2999/2913.Subarrays%20Distinct%20Element%20Sum%20of%20Squares%20I/README.md) | ||
|
||
## Description | ||
|
||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p> | ||
|
||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p> | ||
|
||
<ul> | ||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li> | ||
</ul> | ||
|
||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p> | ||
|
||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,1] | ||
<strong>Output:</strong> 15 | ||
<strong>Explanation:</strong> Six possible subarrays are: | ||
[1]: 1 distinct value | ||
[2]: 1 distinct value | ||
[1]: 1 distinct value | ||
[1,2]: 2 distinct values | ||
[2,1]: 2 distinct values | ||
[1,2,1]: 2 distinct values | ||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,1] | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> Three possible subarrays are: | ||
[1]: 1 distinct value | ||
[1]: 1 distinct value | ||
[1,1]: 1 distinct value | ||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
102 changes: 102 additions & 0 deletions
102
...0-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/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,102 @@ | ||
# [2914. 使二进制字符串变美丽的最少修改次数](https://leetcode.cn/problems/minimum-number-of-changes-to-make-binary-string-beautiful) | ||
|
||
[English Version](/solution/2900-2999/2914.Minimum%20Number%20of%20Changes%20to%20Make%20Binary%20String%20Beautiful/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个长度为偶数下标从 <strong>0</strong> 开始的二进制字符串 <code>s</code> 。</p> | ||
|
||
<p>如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 <strong>美丽的</strong> :</p> | ||
|
||
<ul> | ||
<li>每个子字符串的长度都是 <strong>偶数</strong> 。</li> | ||
<li>每个子字符串都 <strong>只</strong> 包含 <code>1</code> 或 <strong>只</strong> 包含 <code>0</code> 。</li> | ||
</ul> | ||
|
||
<p>你可以将 <code>s</code> 中任一字符改成 <code>0</code> 或者 <code>1</code> 。</p> | ||
|
||
<p>请你返回让字符串 <code>s</code> 美丽的<strong> 最少</strong> 字符修改次数。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>s = "1001" | ||
<b>输出:</b>2 | ||
<b>解释:</b>我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。 | ||
字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。 | ||
将字符串变美丽最少需要 2 次修改。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>s = "10" | ||
<b>输出:</b>1 | ||
<b>解释:</b>我们将 s[1] 改为 1 ,得到字符串 "11" 。 | ||
字符串 "11" 是美丽的,因为它已经是美丽的。 | ||
将字符串变美丽最少需要 1 次修改。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>s = "0000" | ||
<b>输出:</b>0 | ||
<b>解释:</b>不需要进行任何修改,字符串 "0000" 已经是美丽字符串。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li> | ||
<li><code>s</code> 的长度为偶数。</li> | ||
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
92 changes: 92 additions & 0 deletions
92
...999/2914.Minimum Number of Changes to Make Binary String Beautiful/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,92 @@ | ||
# [2914. Minimum Number of Changes to Make Binary String Beautiful](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful) | ||
|
||
[中文文档](/solution/2900-2999/2914.Minimum%20Number%20of%20Changes%20to%20Make%20Binary%20String%20Beautiful/README.md) | ||
|
||
## Description | ||
|
||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p> | ||
|
||
<p>A string is <strong>beautiful</strong> if it's possible to partition it into one or more substrings such that:</p> | ||
|
||
<ul> | ||
<li>Each substring has an <strong>even length</strong>.</li> | ||
<li>Each substring contains <strong>only</strong> <code>1</code>'s or <strong>only</strong> <code>0</code>'s.</li> | ||
</ul> | ||
|
||
<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p> | ||
|
||
<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "1001" | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string "1100". | ||
It can be seen that the string "1100" is beautiful because we can partition it into "11|00". | ||
It can be proven that 2 is the minimum number of changes needed to make the string beautiful. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "10" | ||
<strong>Output:</strong> 1 | ||
<strong>Explanation:</strong> We change s[1] to 1 to get string "11". | ||
It can be seen that the string "11" is beautiful because we can partition it into "11". | ||
It can be proven that 1 is the minimum number of changes needed to make the string beautiful. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "0000" | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> We don't need to make any changes as the string "0000" is beautiful already. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li> | ||
<li><code>s</code> has an even length.</li> | ||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
```java | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
``` | ||
|
||
<!-- tabs:end --> |
Oops, something went wrong.