Skip to content

Commit fc9ce90

Browse files
authored
feat: update lc problems (doocs#3581)
1 parent 24e88e0 commit fc9ce90

File tree

30 files changed

+1413
-35
lines changed

30 files changed

+1413
-35
lines changed

solution/0400-0499/0413.Arithmetic Slices/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0413.Ar
55
tags:
66
- 数组
77
- 动态规划
8+
- 滑动窗口
89
---
910

1011
<!-- problem:start -->

solution/0400-0499/0413.Arithmetic Slices/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0413.Ar
55
tags:
66
- Array
77
- Dynamic Programming
8+
- Sliding Window
89
---
910

1011
<!-- problem:start -->

solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]
3232
<strong>Output:</strong> 13
33-
<strong>Explanation:</strong>
33+
<strong>Explanation:</strong>
3434
There are 6 rectangles of side 1x1.
3535
There are 2 rectangles of side 1x2.
3636
There are 3 rectangles of side 2x1.
37-
There is 1 rectangle of side 2x2.
37+
There is 1 rectangle of side 2x2.
3838
There is 1 rectangle of side 3x1.
3939
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
4040
</pre>
@@ -44,14 +44,14 @@ Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
4444
<pre>
4545
<strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
4646
<strong>Output:</strong> 24
47-
<strong>Explanation:</strong>
47+
<strong>Explanation:</strong>
4848
There are 8 rectangles of side 1x1.
4949
There are 5 rectangles of side 1x2.
50-
There are 2 rectangles of side 1x3.
50+
There are 2 rectangles of side 1x3.
5151
There are 4 rectangles of side 2x1.
52-
There are 2 rectangles of side 2x2.
53-
There are 2 rectangles of side 3x1.
54-
There is 1 rectangle of side 3x2.
52+
There are 2 rectangles of side 2x2.
53+
There are 2 rectangles of side 3x1.
54+
There is 1 rectangle of side 3x2.
5555
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
5656
</pre>
5757

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131

3232
<pre>
3333
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
34-
<strong>输出:</strong>13
34+
<strong>输出:</strong>13
3535
<strong>解释:</strong>所有的子数组和为 1, 3, 6, 10, 2, 5, 9, 3, 7, 4 。将它们升序排序后,我们得到新的数组 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 1 到 ri = 5 的和为 1 + 2 + 3 + 3 + 4 = 13 。
3636
</pre>
3737

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ tags:
3030

3131
<pre>
3232
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
33-
<strong>Output:</strong> 13
34-
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
33+
<strong>Output:</strong> 13
34+
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
3535
</pre>
3636

3737
<p><strong class="example">Example 2:</strong></p>

solution/2700-2799/2765.Longest Alternating Subarray/README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,23 @@ tags:
3333

3434
<p>&nbsp;</p>
3535

36-
<p><strong>示例 1:</strong></p>
36+
<p><strong class="example">示例 1:</strong></p>
3737

38-
<pre>
39-
<b>输入:</b>nums = [2,3,4,3,4]
40-
<b>输出:</b>4
41-
<b>解释:</b>交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。
42-
</pre>
38+
<div class="example-block"><b>输入:</b>nums = [2,3,4,3,4]</div>
4339

44-
<p><strong>示例 2:</strong></p>
40+
<div class="example-block"><b>输出:</b>4</div>
4541

46-
<pre>
47-
<b>输入:</b>nums = [4,5,6]
48-
<b>输出:</b>2
49-
<strong>解释:</strong>[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。
50-
</pre>
42+
<div class="example-block"><b>解释:</b>交替子数组有 <code>[2,3]</code>,<code>[3,4]</code>,<code>[3,4,3]</code> 和 <code>[3,4,3,4]</code>。最长的子数组为 <code>[3,4,3,4]</code>,长度为 4。</div>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong class="example">示例 2:</strong></p>
47+
48+
<div class="example-block"><b>输入:</b>nums = [4,5,6]</div>
49+
50+
<div class="example-block"><b>输出:</b>2</div>
51+
52+
<div class="example-block"><strong>解释:</strong><code>[4,5]</code> 和 <code>[5,6]</code> 是仅有的两个交替子数组。它们长度都为 2 。</div>
5153

5254
<p>&nbsp;</p>
5355

solution/2700-2799/2765.Longest Alternating Subarray/README_EN.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,27 @@ tags:
3434
<p>&nbsp;</p>
3535
<p><strong class="example">Example 1:</strong></p>
3636

37-
<pre>
38-
<strong>Input:</strong> nums = [2,3,4,3,4]
39-
<strong>Output:</strong> 4
40-
<strong>Explanation:</strong> The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
41-
</pre>
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4,3,4]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">4</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<p>The alternating subarrays are <code>[2, 3]</code>, <code>[3,4]</code>, <code>[3,4,3]</code>, and <code>[3,4,3,4]</code>. The longest of these is <code>[3,4,3,4]</code>, which is of length 4.</p>
45+
</div>
4246

4347
<p><strong class="example">Example 2:</strong></p>
4448

45-
<pre>
46-
<strong>Input:</strong> nums = [4,5,6]
47-
<strong>Output:</strong> 2
48-
<strong>Explanation:</strong> [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
49-
</pre>
49+
<div class="example-block">
50+
<p><strong>Input:</strong> <span class="example-io">nums = [4,5,6]</span></p>
51+
52+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
53+
54+
<p><strong>Explanation:</strong></p>
55+
56+
<p><code>[4,5]</code> and <code>[5,6]</code> are the only two alternating subarrays. They are both of length 2.</p>
57+
</div>
5058

5159
<p>&nbsp;</p>
5260
<p><strong>Constraints:</strong></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3300.Minimum%20Element%20After%20Replacement%20With%20Digit%20Sum/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3300. 替换为数位和以后的最小元素](https://leetcode.cn/problems/minimum-element-after-replacement-with-digit-sum)
10+
11+
[English Version](/solution/3300-3399/3300.Minimum%20Element%20After%20Replacement%20With%20Digit%20Sum/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;。</p>
18+
19+
<p>请你将 <code>nums</code>&nbsp;中每一个元素都替换为它的各个数位之 <strong>和</strong>&nbsp;。</p>
20+
21+
<p>请你返回替换所有元素以后 <code>nums</code>&nbsp;中的 <strong>最小</strong>&nbsp;元素。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><span class="example-io"><b>输入:</b>nums = [10,12,13,14]</span></p>
29+
30+
<p><span class="example-io"><b>输出:</b>1</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p><code>nums</code>&nbsp;替换后变为&nbsp;<code>[1, 3, 4, 5]</code>&nbsp;,最小元素为 1 。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4]</span></p>
41+
42+
<p><span class="example-io"><b>输出:</b>1</span></p>
43+
44+
<p><b>解释:</b></p>
45+
46+
<p><code>nums</code>&nbsp;替换后变为&nbsp;<code>[1, 2, 3, 4]</code>&nbsp;,最小元素为 1 。</p>
47+
</div>
48+
49+
<p><strong class="example">示例 3:</strong></p>
50+
51+
<div class="example-block">
52+
<p><span class="example-io"><b>输入:</b>nums = [999,19,199]</span></p>
53+
54+
<p><span class="example-io"><b>输出:</b>10</span></p>
55+
56+
<p><strong>解释:</strong></p>
57+
58+
<p><code>nums</code>&nbsp;替换后变为&nbsp;<code>[27, 10, 19]</code>&nbsp;,最小元素为 10 。</p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
67+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
68+
</ul>
69+
70+
<!-- description:end -->
71+
72+
## 解法
73+
74+
<!-- solution:start -->
75+
76+
### 方法一:模拟
77+
78+
我们可以遍历数组 $\textit{nums}$,对于每个数 $x$,我们计算其各个数位之和 $y$,取所有 $y$ 中的最小值即为答案。
79+
80+
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 $\textit{nums}$ 的长度和数组中的最大值。空间复杂度 $O(1)$。
81+
82+
<!-- tabs:start -->
83+
84+
#### Python3
85+
86+
```python
87+
class Solution:
88+
def minElement(self, nums: List[int]) -> int:
89+
return min(sum(int(b) for b in str(x)) for x in nums)
90+
```
91+
92+
#### Java
93+
94+
```java
95+
class Solution {
96+
public int minElement(int[] nums) {
97+
int ans = 100;
98+
for (int x : nums) {
99+
int y = 0;
100+
for (; x > 0; x /= 10) {
101+
y += x % 10;
102+
}
103+
ans = Math.min(ans, y);
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int minElement(vector<int>& nums) {
116+
int ans = 100;
117+
for (int x : nums) {
118+
int y = 0;
119+
for (; x > 0; x /= 10) {
120+
y += x % 10;
121+
}
122+
ans = min(ans, y);
123+
}
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
#### Go
130+
131+
```go
132+
func minElement(nums []int) int {
133+
ans := 100
134+
for _, x := range nums {
135+
y := 0
136+
for ; x > 0; x /= 10 {
137+
y += x % 10
138+
}
139+
ans = min(ans, y)
140+
}
141+
return ans
142+
}
143+
```
144+
145+
#### TypeScript
146+
147+
```ts
148+
function minElement(nums: number[]): number {
149+
let ans: number = 100;
150+
for (let x of nums) {
151+
let y = 0;
152+
for (; x; x = Math.floor(x / 10)) {
153+
y += x % 10;
154+
}
155+
ans = Math.min(ans, y);
156+
}
157+
return ans;
158+
}
159+
```
160+
161+
<!-- tabs:end -->
162+
163+
<!-- solution:end -->
164+
165+
<!-- problem:end -->

0 commit comments

Comments
 (0)