Skip to content

Commit c51e22f

Browse files
yanglbmeidoocs
andauthored
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 <doocs-bot@outlook.com>
1 parent f346caf commit c51e22f

File tree

24 files changed

+1565
-0
lines changed

24 files changed

+1565
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2913. 子数组不同元素数目的平方和 I](https://leetcode.cn/problems/subarrays-distinct-element-sum-of-squares-i)
2+
3+
[English Version](/solution/2900-2999/2913.Subarrays%20Distinct%20Element%20Sum%20of%20Squares%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。</p>
10+
11+
<p>定义 <code>nums</code>&nbsp;一个子数组的 <strong>不同计数</strong>&nbsp;值如下:</p>
12+
13+
<ul>
14+
<li>令&nbsp;<code>nums[i..j]</code>&nbsp;表示 <code>nums</code> 中所有下标在 <code>i</code> 到 <code>j</code> 范围内的元素构成的子数组(满足 <code>0 &lt;= i &lt;= j &lt; nums.length</code> ),那么我们称子数组&nbsp;<code>nums[i..j]</code>&nbsp;中不同值的数目为&nbsp;<code>nums[i..j]</code>&nbsp;的不同计数。</li>
15+
</ul>
16+
17+
<p>请你返回 <code>nums</code>&nbsp;中所有子数组的 <strong>不同计数</strong>&nbsp;的 <strong>平方</strong>&nbsp;和。</p>
18+
19+
<p>由于答案可能会很大,请你将它对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;后返回。</p>
20+
21+
<p>子数组指的是一个数组里面一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>示例 1:</strong></p>
26+
27+
<pre>
28+
<b>输入:</b>nums = [1,2,1]
29+
<b>输出:</b>15
30+
<b>解释:</b>六个子数组分别为:
31+
[1]: 1 个互不相同的元素。
32+
[2]: 1 个互不相同的元素。
33+
[1]: 1 个互不相同的元素。
34+
[1,2]: 2 个互不相同的元素。
35+
[2,1]: 2 个互不相同的元素。
36+
[1,2,1]: 2 个互不相同的元素。
37+
所有不同计数的平方和为 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 。
38+
</pre>
39+
40+
<p><strong>示例 2:</strong></p>
41+
42+
<pre>
43+
<b>输入:</b>nums = [2,2]
44+
<b>输出:3</b>
45+
<strong>解释:</strong>三个子数组分别为:
46+
[2]: 1 个互不相同的元素。
47+
[2]: 1 个互不相同的元素。
48+
[2,2]: 1 个互不相同的元素。
49+
所有不同计数的平方和为 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3 。
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
58+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
59+
</ul>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
73+
```
74+
75+
### **Java**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```java
80+
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
93+
```
94+
95+
### **...**
96+
97+
```
98+
99+
```
100+
101+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [2913. Subarrays Distinct Element Sum of Squares I](https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i)
2+
3+
[中文文档](/solution/2900-2999/2913.Subarrays%20Distinct%20Element%20Sum%20of%20Squares%20I/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>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
10+
11+
<ul>
12+
<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 &lt;= i &lt;= j &lt; 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>
13+
</ul>
14+
15+
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
16+
17+
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [1,2,1]
24+
<strong>Output:</strong> 15
25+
<strong>Explanation:</strong> Six possible subarrays are:
26+
[1]: 1 distinct value
27+
[2]: 1 distinct value
28+
[1]: 1 distinct value
29+
[1,2]: 2 distinct values
30+
[2,1]: 2 distinct values
31+
[1,2,1]: 2 distinct values
32+
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.
33+
</pre>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> nums = [1,1]
39+
<strong>Output:</strong> 3
40+
<strong>Explanation:</strong> Three possible subarrays are:
41+
[1]: 1 distinct value
42+
[1]: 1 distinct value
43+
[1,1]: 1 distinct value
44+
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>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
52+
</ul>
53+
54+
## Solutions
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
```python
61+
62+
```
63+
64+
### **Java**
65+
66+
```java
67+
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
74+
```
75+
76+
### **Go**
77+
78+
```go
79+
80+
```
81+
82+
### **...**
83+
84+
```
85+
86+
```
87+
88+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [2914. 使二进制字符串变美丽的最少修改次数](https://leetcode.cn/problems/minimum-number-of-changes-to-make-binary-string-beautiful)
2+
3+
[English Version](/solution/2900-2999/2914.Minimum%20Number%20of%20Changes%20to%20Make%20Binary%20String%20Beautiful/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个长度为偶数下标从 <strong>0</strong>&nbsp;开始的二进制字符串&nbsp;<code>s</code>&nbsp;。</p>
10+
11+
<p>如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 <strong>美丽的</strong>&nbsp;:</p>
12+
13+
<ul>
14+
<li>每个子字符串的长度都是 <strong>偶数</strong>&nbsp;。</li>
15+
<li>每个子字符串都 <strong>只</strong>&nbsp;包含 <code>1</code>&nbsp;或 <strong>只</strong>&nbsp;包含 <code>0</code>&nbsp;。</li>
16+
</ul>
17+
18+
<p>你可以将 <code>s</code>&nbsp;中任一字符改成&nbsp;<code>0</code>&nbsp;或者&nbsp;<code>1</code>&nbsp;。</p>
19+
20+
<p>请你返回让字符串 <code>s</code>&nbsp;美丽的<strong>&nbsp;最少</strong>&nbsp;字符修改次数。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<pre>
27+
<b>输入:</b>s = "1001"
28+
<b>输出:</b>2
29+
<b>解释:</b>我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。
30+
字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。
31+
将字符串变美丽最少需要 2 次修改。
32+
</pre>
33+
34+
<p><strong class="example">示例 2:</strong></p>
35+
36+
<pre>
37+
<b>输入:</b>s = "10"
38+
<b>输出:</b>1
39+
<b>解释:</b>我们将 s[1] 改为 1 ,得到字符串 "11" 。
40+
字符串 "11" 是美丽的,因为它已经是美丽的。
41+
将字符串变美丽最少需要 1 次修改。
42+
</pre>
43+
44+
<p><strong class="example">示例 3:</strong></p>
45+
46+
<pre>
47+
<b>输入:</b>s = "0000"
48+
<b>输出:</b>0
49+
<b>解释:</b>不需要进行任何修改,字符串 "0000" 已经是美丽字符串。
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>2 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
58+
<li><code>s</code>&nbsp;的长度为偶数。</li>
59+
<li><code>s[i]</code>&nbsp;要么是&nbsp;<code>'0'</code>&nbsp;,要么是&nbsp;<code>'1'</code> 。</li>
60+
</ul>
61+
62+
## 解法
63+
64+
<!-- 这里可写通用的实现逻辑 -->
65+
66+
<!-- tabs:start -->
67+
68+
### **Python3**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```python
73+
74+
```
75+
76+
### **Java**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```java
81+
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
94+
```
95+
96+
### **...**
97+
98+
```
99+
100+
```
101+
102+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2914. Minimum Number of Changes to Make Binary String Beautiful](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful)
2+
3+
[中文文档](/solution/2900-2999/2914.Minimum%20Number%20of%20Changes%20to%20Make%20Binary%20String%20Beautiful/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p>
8+
9+
<p>A string is <strong>beautiful</strong> if it&#39;s possible to partition it into one or more substrings such that:</p>
10+
11+
<ul>
12+
<li>Each substring has an <strong>even length</strong>.</li>
13+
<li>Each substring contains <strong>only</strong> <code>1</code>&#39;s or <strong>only</strong> <code>0</code>&#39;s.</li>
14+
</ul>
15+
16+
<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p>
17+
18+
<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> s = &quot;1001&quot;
25+
<strong>Output:</strong> 2
26+
<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string &quot;1100&quot;.
27+
It can be seen that the string &quot;1100&quot; is beautiful because we can partition it into &quot;11|00&quot;.
28+
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
29+
</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> s = &quot;10&quot;
35+
<strong>Output:</strong> 1
36+
<strong>Explanation:</strong> We change s[1] to 1 to get string &quot;11&quot;.
37+
It can be seen that the string &quot;11&quot; is beautiful because we can partition it into &quot;11&quot;.
38+
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> s = &quot;0000&quot;
45+
<strong>Output:</strong> 0
46+
<strong>Explanation:</strong> We don&#39;t need to make any changes as the string &quot;0000&quot; is beautiful already.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>2 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
54+
<li><code>s</code> has an even length.</li>
55+
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
56+
</ul>
57+
58+
## Solutions
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
```python
65+
66+
```
67+
68+
### **Java**
69+
70+
```java
71+
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
78+
```
79+
80+
### **Go**
81+
82+
```go
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->

0 commit comments

Comments
 (0)