Skip to content

Commit d56714c

Browse files
authoredFeb 23, 2025··
feat: add weekly contest 438 (#4101)
1 parent f2fccce commit d56714c

File tree

21 files changed

+987
-5
lines changed

21 files changed

+987
-5
lines changed
 

‎solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ tags:
5757
<strong>输出:</strong>
5858
[null,false,true]
5959
<strong>解释:</strong>
60-
FindElements findElements = new FindElements([-1,null,-1]);
61-
findElements.find(1); // return False
60+
FindElements findElements = new FindElements([-1,null,-1]);
61+
findElements.find(1); // return False
6262
findElements.find(2); // return True </pre>
6363

6464
<p><strong class="example">示例 2:</strong></p>

‎solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ tags:
5454
<strong>Output</strong>
5555
[null,false,true]
5656
<strong>Explanation</strong>
57-
FindElements findElements = new FindElements([-1,null,-1]);
58-
findElements.find(1); // return False
57+
FindElements findElements = new FindElements([-1,null,-1]);
58+
findElements.find(1); // return False
5959
findElements.find(2); // return True </pre>
6060

6161
<p><strong class="example">Example 2:</strong></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3461. 判断操作后字符串中的数字是否相等 I](https://leetcode.cn/problems/check-if-digits-are-equal-in-string-after-operations-i)
10+
11+
[English Version](/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个由数字组成的字符串 <code>s</code>&nbsp;。重复执行以下操作,直到字符串恰好包含&nbsp;<strong>两个&nbsp;</strong>数字:</p>
18+
19+
<ul>
20+
<li>从第一个数字开始,对于 <code>s</code> 中的每一对连续数字,计算这两个数字的和&nbsp;<strong>模&nbsp;</strong>10。</li>
21+
<li>用计算得到的新数字依次替换 <code>s</code>&nbsp;的每一个字符,并保持原本的顺序。</li>
22+
</ul>
23+
24+
<p>如果 <code>s</code>&nbsp;最后剩下的两个数字 <strong>相同</strong> ,返回 <code>true</code>&nbsp;。否则,返回 <code>false</code>。</p>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><strong class="example">示例 1:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>输入:</strong> <span class="example-io">s = "3902"</span></p>
32+
33+
<p><strong>输出:</strong> <span class="example-io">true</span></p>
34+
35+
<p><strong>解释:</strong></p>
36+
37+
<ul>
38+
<li>一开始,<code>s = "3902"</code></li>
39+
<li>第一次操作:
40+
<ul>
41+
<li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li>
42+
<li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li>
43+
<li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li>
44+
<li><code>s</code> 变为 <code>"292"</code></li>
45+
</ul>
46+
</li>
47+
<li>第二次操作:
48+
<ul>
49+
<li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li>
50+
<li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li>
51+
<li><code>s</code> 变为 <code>"11"</code></li>
52+
</ul>
53+
</li>
54+
<li>由于 <code>"11"</code> 中的数字相同,输出为 <code>true</code>。</li>
55+
</ul>
56+
</div>
57+
58+
<p><strong class="example">示例 2:</strong></p>
59+
60+
<div class="example-block">
61+
<p><strong>输入:</strong> <span class="example-io">s = "34789"</span></p>
62+
63+
<p><strong>输出:</strong> <span class="example-io">false</span></p>
64+
65+
<p><strong>解释:</strong></p>
66+
67+
<ul>
68+
<li>一开始,<code>s = "34789"</code>。</li>
69+
<li>第一次操作后,<code>s = "7157"</code>。</li>
70+
<li>第二次操作后,<code>s = "862"</code>。</li>
71+
<li>第三次操作后,<code>s = "48"</code>。</li>
72+
<li>由于 <code>'4' != '8'</code>,输出为 <code>false</code>。</li>
73+
</ul>
74+
75+
<p>&nbsp;</p>
76+
</div>
77+
78+
<p><strong>提示:</strong></p>
79+
80+
<ul>
81+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
82+
<li><code>s</code> 仅由数字组成。</li>
83+
</ul>
84+
85+
<!-- description:end -->
86+
87+
## 解法
88+
89+
<!-- solution:start -->
90+
91+
### 方法一
92+
93+
<!-- tabs:start -->
94+
95+
#### Python3
96+
97+
```python
98+
99+
```
100+
101+
#### Java
102+
103+
```java
104+
105+
```
106+
107+
#### C++
108+
109+
```cpp
110+
111+
```
112+
113+
#### Go
114+
115+
```go
116+
117+
```
118+
119+
<!-- tabs:end -->
120+
121+
<!-- solution:end -->
122+
123+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3461. Check If Digits Are Equal in String After Operations I](https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-i)
10+
11+
[中文文档](/solution/3400-3499/3461.Check%20If%20Digits%20Are%20Equal%20in%20String%20After%20Operations%20I/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p>
18+
19+
<ul>
20+
<li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <strong>modulo</strong> 10.</li>
21+
<li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li>
22+
</ul>
23+
24+
<p>Return <code>true</code> if the final two digits in <code>s</code> are the <strong>same</strong>; otherwise, return <code>false</code>.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>Input:</strong> <span class="example-io">s = &quot;3902&quot;</span></p>
31+
32+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<ul>
37+
<li>Initially, <code>s = &quot;3902&quot;</code></li>
38+
<li>First operation:
39+
<ul>
40+
<li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li>
41+
<li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li>
42+
<li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li>
43+
<li><code>s</code> becomes <code>&quot;292&quot;</code></li>
44+
</ul>
45+
</li>
46+
<li>Second operation:
47+
<ul>
48+
<li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li>
49+
<li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li>
50+
<li><code>s</code> becomes <code>&quot;11&quot;</code></li>
51+
</ul>
52+
</li>
53+
<li>Since the digits in <code>&quot;11&quot;</code> are the same, the output is <code>true</code>.</li>
54+
</ul>
55+
</div>
56+
57+
<p><strong class="example">Example 2:</strong></p>
58+
59+
<div class="example-block">
60+
<p><strong>Input:</strong> <span class="example-io">s = &quot;34789&quot;</span></p>
61+
62+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
63+
64+
<p><strong>Explanation:</strong></p>
65+
66+
<ul>
67+
<li>Initially, <code>s = &quot;34789&quot;</code>.</li>
68+
<li>After the first operation, <code>s = &quot;7157&quot;</code>.</li>
69+
<li>After the second operation, <code>s = &quot;862&quot;</code>.</li>
70+
<li>After the third operation, <code>s = &quot;48&quot;</code>.</li>
71+
<li>Since <code>&#39;4&#39; != &#39;8&#39;</code>, the output is <code>false</code>.</li>
72+
</ul>
73+
</div>
74+
75+
<p>&nbsp;</p>
76+
<p><strong>Constraints:</strong></p>
77+
78+
<ul>
79+
<li><code>3 &lt;= s.length &lt;= 100</code></li>
80+
<li><code>s</code> consists of only digits.</li>
81+
</ul>
82+
83+
<!-- description:end -->
84+
85+
## Solutions
86+
87+
<!-- solution:start -->
88+
89+
### Solution 1
90+
91+
<!-- tabs:start -->
92+
93+
#### Python3
94+
95+
```python
96+
97+
```
98+
99+
#### Java
100+
101+
```java
102+
103+
```
104+
105+
#### C++
106+
107+
```cpp
108+
109+
```
110+
111+
#### Go
112+
113+
```go
114+
115+
```
116+
117+
<!-- tabs:end -->
118+
119+
<!-- solution:end -->
120+
121+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3462.Maximum%20Sum%20With%20at%20Most%20K%20Elements/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3462. 提取至多 K 个元素的最大总和](https://leetcode.cn/problems/maximum-sum-with-at-most-k-elements)
10+
11+
[English Version](/solution/3400-3499/3462.Maximum%20Sum%20With%20at%20Most%20K%20Elements/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p data-pm-slice="1 3 []">给你一个大小为 <code>n x m</code>&nbsp;的二维矩阵&nbsp;<code>grid</code>&nbsp;,以及一个长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>limits</code>&nbsp;,和一个整数&nbsp;<code>k</code>&nbsp;。你的目标是从矩阵 <code>grid</code> 中提取出&nbsp;<strong>至多</strong> <code>k</code>&nbsp;个元素,并计算这些元素的最大总和,提取时需满足以下限制<b>:</b></p>
18+
19+
<ul data-spread="false">
20+
<li>
21+
<p>从 <code>grid</code>&nbsp;的第 <code>i</code> 行提取的元素数量不超过 <code>limits[i]</code> 。</p>
22+
</li>
23+
</ul>
24+
25+
<p data-pm-slice="1 1 []">返回最大总和。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><b>示例 1:</b></p>
30+
31+
<div class="example-block">
32+
<p><span class="example-io"><b>输入:</b>grid = [[1,2],[3,4]], limits = [1,2], k = 2</span></p>
33+
34+
<p><span class="example-io"><b>输出:</b>7</span></p>
35+
36+
<p><b>解释:</b></p>
37+
38+
<ul>
39+
<li>从第 2 行提取至多 2 个元素,取出 4 和 3 。</li>
40+
<li>至多提取 2 个元素时的最大总和&nbsp;<code>4 + 3 = 7</code>&nbsp;。</li>
41+
</ul>
42+
</div>
43+
44+
<p><b>示例 2:</b></p>
45+
46+
<div class="example-block">
47+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3</span></p>
48+
49+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">21</span></p>
50+
51+
<p><b>解释:</b></p>
52+
53+
<ul>
54+
<li>从第 1&nbsp;行提取至多 2 个元素,取出 7 。</li>
55+
<li>从第 2 行提取至多 2 个元素,取出&nbsp;8 和 6 。</li>
56+
<li>至多提取 3&nbsp;个元素时的最大总和 <code>7 + 8 + 6 = 21</code>&nbsp;。</li>
57+
</ul>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
62+
<p><b>提示:</b></p>
63+
64+
<ul>
65+
<li><code>n == grid.length == limits.length</code></li>
66+
<li><code>m == grid[i].length</code></li>
67+
<li><code>1 &lt;= n, m &lt;= 500</code></li>
68+
<li><code>0 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>
69+
<li><code>0 &lt;= limits[i] &lt;= m</code></li>
70+
<li><code>0 &lt;= k &lt;= min(n * m, sum(limits))</code></li>
71+
</ul>
72+
73+
<!-- description:end -->
74+
75+
## 解法
76+
77+
<!-- solution:start -->
78+
79+
### 方法一
80+
81+
<!-- tabs:start -->
82+
83+
#### Python3
84+
85+
```python
86+
87+
```
88+
89+
#### Java
90+
91+
```java
92+
93+
```
94+
95+
#### C++
96+
97+
```cpp
98+
99+
```
100+
101+
#### Go
102+
103+
```go
104+
105+
```
106+
107+
<!-- tabs:end -->
108+
109+
<!-- solution:end -->
110+
111+
<!-- problem:end -->

0 commit comments

Comments
 (0)
Please sign in to comment.