Skip to content

Commit 7a0455c

Browse files
authored
feat: update lc problems (#3109)
1 parent b566241 commit 7a0455c

File tree

15 files changed

+110
-95
lines changed

15 files changed

+110
-95
lines changed

lcof2/剑指 Offer II 058. 日程表/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ class MyCalendar {
164164
func book(_ start: Int, _ end: Int) -> Bool {
165165
let newEvent = (start, end)
166166
let index = calendar.firstIndex { $0.0 >= newEvent.1 } ?? calendar.count
167-
167+
168168
if index > 0 && calendar[index - 1].1 > newEvent.0 {
169169
return false
170170
}
171-
171+
172172
calendar.insert(newEvent, at: index)
173173
return true
174174
}

solution/0500-0599/0523.Continuous Subarray Sum/README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ tags:
1919

2020
<!-- description:start -->
2121

22-
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:</p>
22+
<p>给你一个整数数组 <code>nums</code> 和一个整数&nbsp;<code>k</code> ,如果&nbsp;<code>nums</code>&nbsp;有一个 <strong>好的子数组</strong>&nbsp;返回&nbsp;<code>true</code>&nbsp;,否则返回 <code>false</code>:</p>
23+
24+
<p>一个&nbsp;<strong>好的子数组</strong>&nbsp;是:</p>
2325

2426
<ul>
25-
<li>子数组大小 <strong>至少为 2</strong> ,且</li>
27+
<li>长度&nbsp;<strong>至少为 2</strong> ,且</li>
2628
<li>子数组元素总和为 <code>k</code> 的倍数。</li>
2729
</ul>
2830

29-
<p>如果存在,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
31+
<p><strong>注意</strong>:</p>
3032

31-
<p>如果存在一个整数 <code>n</code> ,令整数 <code>x</code> 符合 <code>x = n * k</code> ,则称 <code>x</code> 是 <code>k</code> 的一个倍数。<code>0</code> 始终视为 <code>k</code> 的一个倍数。</p>
33+
<ul>
34+
<li><strong>子数组</strong> 是数组中 <strong>连续</strong> 的部分。</li>
35+
<li>如果存在一个整数 <code>n</code> ,令整数 <code>x</code> 符合 <code>x = n * k</code> ,则称 <code>x</code> 是 <code>k</code> 的一个倍数。<code>0</code> <strong>始终</strong> 视为 <code>k</code> 的一个倍数。</li>
36+
</ul>
3237

33-
<p> </p>
38+
<p>&nbsp;</p>
3439

3540
<p><strong>示例 1:</strong></p>
3641

@@ -55,15 +60,15 @@ tags:
5560
<strong>输出:</strong>false
5661
</pre>
5762

58-
<p> </p>
63+
<p>&nbsp;</p>
5964

6065
<p><strong>提示:</strong></p>
6166

6267
<ul>
63-
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
64-
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
65-
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
66-
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
68+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
69+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
70+
<li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li>
71+
<li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li>
6772
</ul>
6873

6974
<!-- description:end -->

solution/0900-0999/0974.Subarray Sums Divisible by K/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>给定一个整数数组 <code>nums</code>&nbsp;和一个整数 <code>k</code> ,返回其中元素之和可被 <code>k</code>&nbsp;整除的(连续、非空) <strong>子数组</strong> 的数目。</p>
21+
<p>给定一个整数数组 <code>nums</code>&nbsp;和一个整数 <code>k</code> ,返回其中元素之和可被 <code>k</code>&nbsp;整除的非空&nbsp;<strong>子数组</strong> 的数目。</p>
2222

23-
<p><strong>子数组</strong> 是数组的 <strong>连续</strong> 部分。</p>
23+
<p><strong>子数组</strong> 是数组中&nbsp;<strong>连续</strong>&nbsp;的部分。</p>
2424

2525
<p>&nbsp;</p>
2626

solution/1000-1099/1090.Largest Values From Labels/README.md

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,54 @@ tags:
2222

2323
<!-- description:start -->
2424

25-
<p>我们有一个&nbsp;<code>n</code>&nbsp;项的集合。给出两个整数数组&nbsp;<code>values</code>&nbsp;和 <code>labels</code>&nbsp;,第 <code>i</code> 个元素的值和标签分别是&nbsp;<code>values[i]</code>&nbsp;&nbsp;<code>labels[i]</code>。还会给出两个整数&nbsp;<code>numWanted</code>&nbsp;和 <code>useLimit</code> 。</p>
25+
<p>以两个整数数组 &nbsp;<code>values</code>&nbsp;和 <code>labels</code>&nbsp;给定&nbsp;<code>n</code>&nbsp;个项的值和标签,并且给出两个整数&nbsp;<code>numWanted</code>&nbsp;和 <code>useLimit</code> 。</p>
2626

27-
<p>从 <code>n</code> 个元素中选择一个子集 <code>s</code> :</p>
27+
<p>你的任务是从这些项中找到一个值的和 <strong>最大</strong> 的子集使得:</p>
2828

2929
<ul>
30-
<li>子集 <code>s</code> 的大小&nbsp;<strong>小于或等于</strong> <code>numWanted</code> 。</li>
31-
<li><code>s</code> 中 <strong>最多</strong> 有相同标签的 <code>useLimit</code>。</li>
30+
<li>项的数量 <strong>最多</strong> 为&nbsp;<code>numWanted</code>。</li>
31+
<li>相同标签的项的数量&nbsp;<strong>最多 </strong>为&nbsp;<code>useLimit</code>。</li>
3232
</ul>
3333

34-
<p>一个子集的&nbsp;<strong>分数&nbsp;</strong>是该子集的值之和。</p>
35-
36-
<p>返回子集&nbsp;<code>s</code> 的最大 <strong>分数</strong> 。</p>
34+
<p>返回最大的和。</p>
3735

3836
<p>&nbsp;</p>
3937

40-
<p><strong>示例 1:</strong></p>
38+
<p><strong class="example">示例 1:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>输入:</strong><span class="example-io">values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1</span></p>
42+
43+
<p><strong>输出:</strong><span class="example-io">9</span></p>
44+
45+
<p><strong>解释:</strong></p>
46+
47+
<p>选择的子集是第一个、第三个和第五个项,其值之和为 5 + 3 + 1。</p>
48+
</div>
49+
50+
<p><strong class="example">示例 2:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>输入:</strong><span class="example-io">values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2</span></p>
54+
55+
<p><strong>输出:</strong><span class="example-io">12</span></p>
56+
57+
<p><strong>解释:</strong></p>
58+
59+
<p>选择的子集是第一个、第二个和第三个项,其值之和为 5 + 4 + 3。</p>
60+
</div>
4161

42-
<pre>
43-
<strong>输入:</strong>values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
44-
<strong>输出:</strong>9
45-
<strong>解释:</strong>选出的子集是第一项,第三项和第五项。
46-
</pre>
62+
<p><strong class="example">示例 3:</strong></p>
4763

48-
<p><strong>示例 2:</strong></p>
64+
<div class="example-block">
65+
<p><strong>输入:</strong><span class="example-io">values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1</span></p>
4966

50-
<pre>
51-
<strong>输入:</strong>values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
52-
<strong>输出:</strong>12
53-
<strong>解释:</strong>选出的子集是第一项,第二项和第三项。
54-
</pre>
67+
<p><strong>输出:</strong><span class="example-io">16</span></p>
5568

56-
<p><strong>示例 3:</strong></p>
69+
<p><strong>解释:</strong></p>
5770

58-
<pre>
59-
<strong>输入:</strong>values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1
60-
<strong>输出:</strong>16
61-
<strong>解释:</strong>选出的子集是第一项和第四项。
62-
</pre>
71+
<p>选择的子集是第一个和第四个项,其值之和为 9 + 7。</p>
72+
</div>
6373

6474
<p>&nbsp;</p>
6575

solution/1200-1299/1240.Tiling a Rectangle with the Fewest Squares/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,30 @@ tags:
3030

3131
<p><strong>示例 1:</strong></p>
3232

33-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1240.Tiling%20a%20Rectangle%20with%20the%20Fewest%20Squares/images/sample_11_1592.png" style="height: 106px; width: 154px;"></p>
33+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1240.Tiling%20a%20Rectangle%20with%20the%20Fewest%20Squares/images/sample_11_1592.png" style="height: 106px; width: 154px;" /></p>
3434

35-
<pre><strong>输入:</strong>n = 2, m = 3
35+
<pre>
36+
<strong>输入:</strong>n = 2, m = 3
3637
<strong>输出:</strong>3
3738
<code><strong>解释:</strong>3</code> 块地砖就可以铺满卧室。
3839
<code> 2</code> 块 <code>1x1 地砖</code>
3940
<code> 1</code> 块 <code>2x2 地砖</code></pre>
4041

4142
<p><strong>示例 2:</strong></p>
4243

43-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1240.Tiling%20a%20Rectangle%20with%20the%20Fewest%20Squares/images/sample_22_1592.png" style="height: 126px; width: 224px;"></p>
44+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1240.Tiling%20a%20Rectangle%20with%20the%20Fewest%20Squares/images/sample_22_1592.png" style="height: 126px; width: 224px;" /></p>
4445

45-
<pre><strong>输入:</strong>n = 5, m = 8
46+
<pre>
47+
<strong>输入:</strong>n = 5, m = 8
4648
<strong>输出:</strong>5
4749
</pre>
4850

4951
<p><strong>示例 3:</strong></p>
5052

51-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1240.Tiling%20a%20Rectangle%20with%20the%20Fewest%20Squares/images/sample_33_1592.png" style="height: 189px; width: 224px;"></p>
53+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1240.Tiling%20a%20Rectangle%20with%20the%20Fewest%20Squares/images/sample_33_1592.png" style="height: 189px; width: 224px;" /></p>
5254

53-
<pre><strong>输入:</strong>n = 11, m = 13
55+
<pre>
56+
<strong>输入:</strong>n = 11, m = 13
5457
<strong>输出:</strong>6
5558
</pre>
5659

@@ -59,8 +62,7 @@ tags:
5962
<p><strong>提示:</strong></p>
6063

6164
<ul>
62-
<li><code>1 &lt;= n &lt;= 13</code></li>
63-
<li><code>1 &lt;= m&nbsp;&lt;=&nbsp;13</code></li>
65+
<li><code>1 &lt;= n, m &lt;= 13</code></li>
6466
</ul>
6567

6668
<!-- description:end -->

solution/1500-1599/1597.Build Binary Expression Tree From Infix Expression/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ tags:
6666

6767
<ul>
6868
<li><code>1 &lt;= s.length &lt;= 100</code></li>
69-
<li><code>s</code>&nbsp;中包含数字和字符&nbsp;<code>'+'</code>、&nbsp;<code>'-'</code>、&nbsp;<code>'*'</code>、&nbsp;<code>'/'</code> 。</li>
69+
<li><code>s</code>&nbsp;中包含数字和字符&nbsp;<code>'('</code>、&nbsp;<code>')'</code>、<code>'+'</code>、&nbsp;<code>'-'</code>、&nbsp;<code>'*'</code>、&nbsp;<code>'/'</code> 。</li>
7070
<li><code>s</code>&nbsp;中的操作数 <strong>恰好</strong> 是一位数字。</li>
7171
<li>题目数据保证 <code>s</code> 是一个有效的表达式。</li>
7272
</ul>

solution/1500-1599/1597.Build Binary Expression Tree From Infix Expression/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The third tree below is also not valid. Although it produces the same result and
6565

6666
<ul>
6767
<li><code>1 &lt;= s.length &lt;= 100</code></li>
68-
<li><code>s</code> consists of digits and the characters <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;*&#39;</code>, and <code>&#39;/&#39;</code>.</li>
68+
<li><code>s</code> consists of digits and the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;*&#39;</code>, and <code>&#39;/&#39;</code>.</li>
6969
<li>Operands in <code>s</code> are <strong>exactly</strong> 1 digit.</li>
7070
<li>It is guaranteed that <code>s</code> is a valid expression.</li>
7171
</ul>

solution/2700-2799/2779.Maximum Beauty of an Array After Applying Operation/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ tags:
8080

8181
我们注意到,对于每一次操作,区间 $[nums[i]-k, nums[i]+k]$ 内的所有元素都会增加 $1$,因此我们可以使用差分数组来记录这些操作对美丽值的贡献。
8282

83-
题目中 $nums[i]-k$ 可能为负数,我们统一将所有元素加上 $k$,保证结果为非负数。因此,我们需要创建一个长度为 $\max(nums) + k \times 2 + 2$ 的差分数组 $d$。
83+
题目中 $nums[i]-k$ 可能为负数,我们统一将所有元素加上 $k$,保证结果为非负数。因此,我们可以创建一个长度为 $\max(nums) + k \times 2 + 2$ 的差分数组 $d$。
8484

8585
接下来,遍历数组 $nums$,对于当前遍历到的元素 $x$,我们将 $d[x]$ 增加 $1$,将 $d[x+k\times2+1]$ 减少 $1$。这样,我们就可以通过 $d$ 数组计算出每个位置的前缀和,即为每个位置的美丽值。找到最大的美丽值即可。
8686

87-
时间复杂度 $O(n)$,空间复杂度 $O(M + 2 \times k)$。其中 $n$ 是数组 $nums$ 的长度,而 $M$ 是数组 $nums$ 中的最大值。
87+
时间复杂度 $O(M + 2 \times k + n)$,空间复杂度 $O(M + 2 \times k)$。其中 $n$ 是数组 $nums$ 的长度,而 $M$ 是数组 $nums$ 中的最大值。
8888

8989
<!-- tabs:start -->
9090

@@ -98,11 +98,7 @@ class Solution:
9898
for x in nums:
9999
d[x] += 1
100100
d[x + k * 2 + 1] -= 1
101-
ans = s = 0
102-
for x in d:
103-
s += x
104-
ans = max(ans, s)
105-
return ans
101+
return max(accumulate(d))
106102
```
107103

108104
#### Java
@@ -175,7 +171,7 @@ func maximumBeauty(nums []int, k int) (ans int) {
175171
```ts
176172
function maximumBeauty(nums: number[], k: number): number {
177173
const m = Math.max(...nums) + k * 2 + 2;
178-
const d: number[] = new Array(m).fill(0);
174+
const d: number[] = Array(m).fill(0);
179175
for (const x of nums) {
180176
d[x]++;
181177
d[x + k * 2 + 1]--;

solution/2700-2799/2779.Maximum Beauty of an Array After Applying Operation/README_EN.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ The beauty of the array nums is 4 (whole array).
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1
77+
### Solution 1: Difference Array
78+
79+
We notice that for each operation, all elements within the interval $[nums[i]-k, nums[i]+k]$ will increase by $1$. Therefore, we can use a difference array to record the contributions of these operations to the beauty value.
80+
81+
In the problem, $nums[i]-k$ might be negative. We add $k$ to all elements to ensure the results are non-negative. Thus, we can create a difference array $d$ with a length of $\max(nums) + k \times 2 + 2$.
82+
83+
Next, we iterate through the array $nums$. For the current element $x$ being iterated, we increase $d[x]$ by $1$ and decrease $d[x+k\times2+1]$ by $1$. In this way, we can calculate the prefix sum for each position using the $d$ array, which represents the beauty value for each position. The maximum beauty value can then be found.
84+
85+
The time complexity is $O(M + 2 \times k + n)$, and the space complexity is $O(M + 2 \times k)$. Here, $n$ is the length of the array $nums$, and $M$ is the maximum value in the array $nums$.
7886

7987
<!-- tabs:start -->
8088

@@ -88,11 +96,7 @@ class Solution:
8896
for x in nums:
8997
d[x] += 1
9098
d[x + k * 2 + 1] -= 1
91-
ans = s = 0
92-
for x in d:
93-
s += x
94-
ans = max(ans, s)
95-
return ans
99+
return max(accumulate(d))
96100
```
97101

98102
#### Java
@@ -165,7 +169,7 @@ func maximumBeauty(nums []int, k int) (ans int) {
165169
```ts
166170
function maximumBeauty(nums: number[], k: number): number {
167171
const m = Math.max(...nums) + k * 2 + 2;
168-
const d: number[] = new Array(m).fill(0);
172+
const d: number[] = Array(m).fill(0);
169173
for (const x of nums) {
170174
d[x]++;
171175
d[x + k * 2 + 1]--;

solution/2700-2799/2779.Maximum Beauty of an Array After Applying Operation/Solution.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@ def maximumBeauty(self, nums: List[int], k: int) -> int:
55
for x in nums:
66
d[x] += 1
77
d[x + k * 2 + 1] -= 1
8-
ans = s = 0
9-
for x in d:
10-
s += x
11-
ans = max(ans, s)
12-
return ans
8+
return max(accumulate(d))

0 commit comments

Comments
 (0)