Skip to content

Commit ecece7a

Browse files
yanglbmeidoocs
andauthored
feat: add weekly contest 384 (doocs#2336)
Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
1 parent 6fd076f commit ecece7a

File tree

14 files changed

+628
-0
lines changed

14 files changed

+628
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [3033. 修改矩阵](https://leetcode.cn/problems/modify-the-matrix)
2+
3+
[English Version](/solution/3000-3099/3033.Modify%20the%20Matrix/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的整数矩阵 <code>matrix</code> ,新建一个下标从 <strong>0</strong> 开始、名为 <code>answer</code> 的矩阵。使 <code>answer</code> 与 <code>matrix</code> 相等,接着将其中每个值为 <code>-1</code> 的元素替换为所在列的 <strong>最大</strong> 元素。</p>
10+
11+
<p>返回矩阵 <code>answer</code> 。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong class="example">示例 1:</strong></p>
16+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3033.Modify%20the%20Matrix/images/matrix1.png" style="width: 491px; height: 161px;" />
17+
<pre>
18+
<strong>输入:</strong>matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
19+
<strong>输出:</strong>[[1,2,9],[4,8,6],[7,8,9]]
20+
<strong>解释:</strong>上图显示了发生替换的元素(蓝色区域)。
21+
- 将单元格 [1][1] 中的值替换为列 1 中的最大值 8 。
22+
- 将单元格 [0][2] 中的值替换为列 2 中的最大值 9 。
23+
</pre>
24+
25+
<p><strong class="example">示例 2:</strong></p>
26+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3033.Modify%20the%20Matrix/images/matrix2.png" style="width: 411px; height: 111px;" />
27+
<pre>
28+
<strong>输入:</strong>matrix = [[3,-1],[5,2]]
29+
<strong>输出:</strong>[[3,2],[5,2]]
30+
<strong>解释:</strong>上图显示了发生替换的元素(蓝色区域)。
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>提示:</strong></p>
36+
37+
<ul>
38+
<li><code>m == matrix.length</code></li>
39+
<li><code>n == matrix[i].length</code></li>
40+
<li><code>2 &lt;= m, n &lt;= 50</code></li>
41+
<li><code>-1 &lt;= matrix[i][j] &lt;= 100</code></li>
42+
<li>测试用例中生成的输入满足每列至少包含一个非负整数。</li>
43+
</ul>
44+
45+
## 解法
46+
47+
### 方法一
48+
49+
<!-- tabs:start -->
50+
51+
```python
52+
53+
```
54+
55+
```java
56+
57+
```
58+
59+
```cpp
60+
61+
```
62+
63+
```go
64+
65+
```
66+
67+
<!-- tabs:end -->
68+
69+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [3033. Modify the Matrix](https://leetcode.com/problems/modify-the-matrix)
2+
3+
[中文文档](/solution/3000-3099/3033.Modify%20the%20Matrix/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>matrix</code>, create a new <strong>0-indexed</strong> matrix called <code>answer</code>. Make <code>answer</code> equal to <code>matrix</code>, then replace each element with the value <code>-1</code> with the <strong>maximum</strong> element in its respective column.</p>
8+
9+
<p>Return <em>the matrix</em> <code>answer</code>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3033.Modify%20the%20Matrix/images/matrix1.png" style="width: 491px; height: 161px;" />
14+
<pre>
15+
<strong>Input:</strong> matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
16+
<strong>Output:</strong> [[1,2,9],[4,8,6],[7,8,9]]
17+
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
18+
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8.
19+
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3033.Modify%20the%20Matrix/images/matrix2.png" style="width: 411px; height: 111px;" />
24+
<pre>
25+
<strong>Input:</strong> matrix = [[3,-1],[5,2]]
26+
<strong>Output:</strong> [[3,2],[5,2]]
27+
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>m == matrix.length</code></li>
35+
<li><code>n == matrix[i].length</code></li>
36+
<li><code>2 &lt;= m, n &lt;= 50</code></li>
37+
<li><code>-1 &lt;= matrix[i][j] &lt;= 100</code></li>
38+
<li>The input is generated such that each column contains at least one non-negative integer.</li>
39+
</ul>
40+
41+
## Solutions
42+
43+
### Solution 1
44+
45+
<!-- tabs:start -->
46+
47+
```python
48+
49+
```
50+
51+
```java
52+
53+
```
54+
55+
```cpp
56+
57+
```
58+
59+
```go
60+
61+
```
62+
63+
<!-- tabs:end -->
64+
65+
<!-- end -->
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [3034. 匹配模式数组的子数组数目 I](https://leetcode.cn/problems/number-of-subarrays-that-match-a-pattern-i)
2+
3+
[English Version](/solution/3000-3099/3034.Number%20of%20Subarrays%20That%20Match%20a%20Pattern%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;,和一个下标从 <code>0</code>&nbsp;开始长度为 <code>m</code>&nbsp;的整数数组&nbsp;<code>pattern</code>&nbsp;,<code>pattern</code>&nbsp;数组只包含整数&nbsp;<code>-1</code>&nbsp;,<code>0</code>&nbsp;&nbsp;<code>1</code>&nbsp;。</p>
10+
11+
<p>大小为 <code>m + 1</code>&nbsp;的<span data-keyword="subarray">子数组</span>&nbsp;<code>nums[i..j]</code>&nbsp;如果对于每个元素 <code>pattern[k]</code>&nbsp;都满足以下条件,那么我们说这个子数组匹配模式数组&nbsp;<code>pattern</code>&nbsp;:</p>
12+
13+
<ul>
14+
<li>如果 <code>pattern[k] == 1</code> ,那么 <code>nums[i + k + 1] &gt; nums[i + k]</code></li>
15+
<li>如果&nbsp;<code>pattern[k] == 0</code>&nbsp;,那么&nbsp;<code>nums[i + k + 1] == nums[i + k]</code></li>
16+
<li>如果&nbsp;<code>pattern[k] == -1</code>&nbsp;,那么&nbsp;<code>nums[i + k + 1] &lt; nums[i + k]</code></li>
17+
</ul>
18+
19+
<p>请你返回匹配 <code>pattern</code>&nbsp;的 <code>nums</code>&nbsp;子数组的 <strong>数目</strong>&nbsp;。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<pre>
26+
<b>输入:</b>nums = [1,2,3,4,5,6], pattern = [1,1]
27+
<b>输出:</b>4
28+
<b>解释:</b>模式 [1,1] 说明我们要找的子数组是长度为 3 且严格上升的。在数组 nums 中,子数组 [1,2,3] ,[2,3,4] ,[3,4,5] 和 [4,5,6] 都匹配这个模式。
29+
所以 nums 中总共有 4 个子数组匹配这个模式。
30+
</pre>
31+
32+
<p><strong class="example">示例 2:</strong></p>
33+
34+
<pre>
35+
<b>输入:</b>nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
36+
<b>输出:</b>2
37+
<strong>解释:</strong>这里,模式数组 [1,0,-1] 说明我们需要找的子数组中,第一个元素小于第二个元素,第二个元素等于第三个元素,第三个元素大于第四个元素。在 nums 中,子数组 [1,4,4,1] 和 [3,5,5,3] 都匹配这个模式。
38+
所以 nums 中总共有 2 个子数组匹配这个模式。
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
43+
<p><strong>提示:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= n == nums.length &lt;= 100</code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
48+
<li><code>1 &lt;= m == pattern.length &lt; n</code></li>
49+
<li><code>-1 &lt;= pattern[i] &lt;= 1</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
### 方法一
55+
56+
<!-- tabs:start -->
57+
58+
```python
59+
60+
```
61+
62+
```java
63+
64+
```
65+
66+
```cpp
67+
68+
```
69+
70+
```go
71+
72+
```
73+
74+
<!-- tabs:end -->
75+
76+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [3034. Number of Subarrays That Match a Pattern I](https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i)
2+
3+
[中文文档](/solution/3000-3099/3034.Number%20of%20Subarrays%20That%20Match%20a%20Pattern%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
8+
9+
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
10+
11+
<ul>
12+
<li><code>nums[i + k + 1] &gt; nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
13+
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
14+
<li><code>nums[i + k + 1] &lt; nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
15+
</ul>
16+
17+
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</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,3,4,5,6], pattern = [1,1]
24+
<strong>Output:</strong> 4
25+
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
26+
Hence, there are 4 subarrays in nums that match the pattern.
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
33+
<strong>Output:</strong> 2
34+
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
35+
Hence, there are 2 subarrays in nums that match the pattern.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>2 &lt;= n == nums.length &lt;= 100</code></li>
43+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
44+
<li><code>1 &lt;= m == pattern.length &lt; n</code></li>
45+
<li><code>-1 &lt;= pattern[i] &lt;= 1</code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
### Solution 1
51+
52+
<!-- tabs:start -->
53+
54+
```python
55+
56+
```
57+
58+
```java
59+
60+
```
61+
62+
```cpp
63+
64+
```
65+
66+
```go
67+
68+
```
69+
70+
<!-- tabs:end -->
71+
72+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [3035. 回文字符串的最大数量](https://leetcode.cn/problems/maximum-palindromes-after-operations)
2+
3+
[English Version](/solution/3000-3099/3035.Maximum%20Palindromes%20After%20Operations/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> ,数组的长度为 <code>n</code> ,且包含下标从 <strong>0</strong> 开始的若干字符串。</p>
10+
11+
<p>你可以执行以下操作 <strong>任意 </strong>次数(<strong>包括零次</strong>):</p>
12+
13+
<ul>
14+
<li>选择整数<code>i</code>、<code>j</code>、<code>x</code>和<code>y</code>,满足<code>0 &lt;= i, j &lt; n</code>,<code>0 &lt;= x &lt; words[i].length</code>,<code>0 &lt;= y &lt; words[j].length</code>,<strong>交换 </strong>字符 <code>words[i][x]</code> 和 <code>words[j][y]</code> 。</li>
15+
</ul>
16+
17+
<p>返回一个整数,表示在执行一些操作后,<code>words</code> 中可以包含的回文字符串的 <strong>最大 </strong>数量。</p>
18+
19+
<p><strong>注意:</strong>在操作过程中,<code>i</code> 和 <code>j</code> 可以相等。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<pre>
26+
<strong>输入:</strong>words = ["abbb","ba","aa"]
27+
<strong>输出:</strong>3
28+
<strong>解释:</strong>在这个例子中,获得最多回文字符串的一种方式是:
29+
选择 i = 0, j = 1, x = 0, y = 0,交换 words[0][0] 和 words[1][0] 。words 变成了 ["bbbb","aa","aa"] 。
30+
words 中的所有字符串都是回文。
31+
因此,可实现的回文字符串的最大数量是 3 。
32+
</pre>
33+
34+
<p><strong class="example">示例 2:</strong></p>
35+
36+
<pre>
37+
<strong>输入:</strong>words = ["abc","ab"]
38+
<strong>输出:</strong>2
39+
<strong>解释:</strong>在这个例子中,获得最多回文字符串的一种方式是:
40+
选择 i = 0, j = 1, x = 1, y = 0,交换 words[0][1] 和 words[1][0] 。words 变成了 ["aac","bb"] 。
41+
选择 i = 0, j = 0, x = 1, y = 2,交换 words[0][1] 和 words[0][2] 。words 变成了 ["aca","bb"] 。
42+
两个字符串都是回文 。
43+
因此,可实现的回文字符串的最大数量是 2。
44+
</pre>
45+
46+
<p><strong class="example">示例 3:</strong></p>
47+
48+
<pre>
49+
<strong>输入:</strong>words = ["cd","ef","a"]
50+
<strong>输出:</strong>1
51+
<strong>解释:</strong>在这个例子中,没有必要执行任何操作。
52+
words 中有一个回文 "a" 。
53+
可以证明,在执行任何次数操作后,无法得到更多回文。
54+
因此,答案是 1 。
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
59+
<p><strong>提示:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
63+
<li><code>1 &lt;= words[i].length &lt;= 100</code></li>
64+
<li><code>words[i]</code> 仅由小写英文字母组成。</li>
65+
</ul>
66+
67+
## 解法
68+
69+
### 方法一
70+
71+
<!-- tabs:start -->
72+
73+
```python
74+
75+
```
76+
77+
```java
78+
79+
```
80+
81+
```cpp
82+
83+
```
84+
85+
```go
86+
87+
```
88+
89+
<!-- tabs:end -->
90+
91+
<!-- end -->

0 commit comments

Comments
 (0)