Skip to content

feat: update lc problems #3109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lcof2/剑指 Offer II 058. 日程表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ class MyCalendar {
func book(_ start: Int, _ end: Int) -> Bool {
let newEvent = (start, end)
let index = calendar.firstIndex { $0.0 >= newEvent.1 } ?? calendar.count

if index > 0 && calendar[index - 1].1 > newEvent.0 {
return false
}

calendar.insert(newEvent, at: index)
return true
}
Expand Down
25 changes: 15 additions & 10 deletions solution/0500-0599/0523.Continuous Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ tags:

<!-- description:start -->

<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:</p>
<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>

<p>一个&nbsp;<strong>好的子数组</strong>&nbsp;是:</p>

<ul>
<li>子数组大小 <strong>至少为 2</strong> ,且</li>
<li>长度&nbsp;<strong>至少为 2</strong> ,且</li>
<li>子数组元素总和为 <code>k</code> 的倍数。</li>
</ul>

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

<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>
<ul>
<li><strong>子数组</strong> 是数组中 <strong>连续</strong> 的部分。</li>
<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>
</ul>

<p> </p>
<p>&nbsp;</p>

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

Expand All @@ -55,15 +60,15 @@ tags:
<strong>输出:</strong>false
</pre>

<p> </p>
<p>&nbsp;</p>

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

<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li>
<li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ tags:

<!-- description:start -->

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

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

<p>&nbsp;</p>

Expand Down
60 changes: 35 additions & 25 deletions solution/1000-1099/1090.Largest Values From Labels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,54 @@ tags:

<!-- description:start -->

<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>
<p>以两个整数数组 &nbsp;<code>values</code>&nbsp;和 <code>labels</code>&nbsp;给定&nbsp;<code>n</code>&nbsp;个项的值和标签,并且给出两个整数&nbsp;<code>numWanted</code>&nbsp;和 <code>useLimit</code> 。</p>

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

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

<p>一个子集的&nbsp;<strong>分数&nbsp;</strong>是该子集的值之和。</p>

<p>返回子集&nbsp;<code>s</code> 的最大 <strong>分数</strong> 。</p>
<p>返回最大的和。</p>

<p>&nbsp;</p>

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

<div class="example-block">
<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>

<p><strong>输出:</strong><span class="example-io">9</span></p>

<p><strong>解释:</strong></p>

<p>选择的子集是第一个、第三个和第五个项,其值之和为 5 + 3 + 1。</p>
</div>

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

<div class="example-block">
<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>

<p><strong>输出:</strong><span class="example-io">12</span></p>

<p><strong>解释:</strong></p>

<p>选择的子集是第一个、第二个和第三个项,其值之和为 5 + 4 + 3。</p>
</div>

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

<p><strong>示例 2:</strong></p>
<div class="example-block">
<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>

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

<p><strong>示例 3:</strong></p>
<p><strong>解释:</strong></p>

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

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,30 @@ tags:

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

<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>
<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>

<pre><strong>输入:</strong>n = 2, m = 3
<pre>
<strong>输入:</strong>n = 2, m = 3
<strong>输出:</strong>3
<code><strong>解释:</strong>3</code> 块地砖就可以铺满卧室。
<code> 2</code> 块 <code>1x1 地砖</code>
<code> 1</code> 块 <code>2x2 地砖</code></pre>

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

<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>
<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>

<pre><strong>输入:</strong>n = 5, m = 8
<pre>
<strong>输入:</strong>n = 5, m = 8
<strong>输出:</strong>5
</pre>

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

<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>
<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>

<pre><strong>输入:</strong>n = 11, m = 13
<pre>
<strong>输入:</strong>n = 11, m = 13
<strong>输出:</strong>6
</pre>

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

<ul>
<li><code>1 &lt;= n &lt;= 13</code></li>
<li><code>1 &lt;= m&nbsp;&lt;=&nbsp;13</code></li>
<li><code>1 &lt;= n, m &lt;= 13</code></li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ tags:

<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code>&nbsp;中包含数字和字符&nbsp;<code>'+'</code>、&nbsp;<code>'-'</code>、&nbsp;<code>'*'</code>、&nbsp;<code>'/'</code> 。</li>
<li><code>s</code>&nbsp;中包含数字和字符&nbsp;<code>'('</code>、&nbsp;<code>')'</code>、<code>'+'</code>、&nbsp;<code>'-'</code>、&nbsp;<code>'*'</code>、&nbsp;<code>'/'</code> 。</li>
<li><code>s</code>&nbsp;中的操作数 <strong>恰好</strong> 是一位数字。</li>
<li>题目数据保证 <code>s</code> 是一个有效的表达式。</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The third tree below is also not valid. Although it produces the same result and

<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<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>
<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>
<li>Operands in <code>s</code> are <strong>exactly</strong> 1 digit.</li>
<li>It is guaranteed that <code>s</code> is a valid expression.</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ tags:

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

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

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

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

<!-- tabs:start -->

Expand All @@ -98,11 +98,7 @@ class Solution:
for x in nums:
d[x] += 1
d[x + k * 2 + 1] -= 1
ans = s = 0
for x in d:
s += x
ans = max(ans, s)
return ans
return max(accumulate(d))
```

#### Java
Expand Down Expand Up @@ -175,7 +171,7 @@ func maximumBeauty(nums []int, k int) (ans int) {
```ts
function maximumBeauty(nums: number[], k: number): number {
const m = Math.max(...nums) + k * 2 + 2;
const d: number[] = new Array(m).fill(0);
const d: number[] = Array(m).fill(0);
for (const x of nums) {
d[x]++;
d[x + k * 2 + 1]--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ The beauty of the array nums is 4 (whole array).

<!-- solution:start -->

### Solution 1
### Solution 1: Difference Array

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.

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$.

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.

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$.

<!-- tabs:start -->

Expand All @@ -88,11 +96,7 @@ class Solution:
for x in nums:
d[x] += 1
d[x + k * 2 + 1] -= 1
ans = s = 0
for x in d:
s += x
ans = max(ans, s)
return ans
return max(accumulate(d))
```

#### Java
Expand Down Expand Up @@ -165,7 +169,7 @@ func maximumBeauty(nums []int, k int) (ans int) {
```ts
function maximumBeauty(nums: number[], k: number): number {
const m = Math.max(...nums) + k * 2 + 2;
const d: number[] = new Array(m).fill(0);
const d: number[] = Array(m).fill(0);
for (const x of nums) {
d[x]++;
d[x + k * 2 + 1]--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ def maximumBeauty(self, nums: List[int], k: int) -> int:
for x in nums:
d[x] += 1
d[x + k * 2 + 1] -= 1
ans = s = 0
for x in d:
s += x
ans = max(ans, s)
return ans
return max(accumulate(d))
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function maximumBeauty(nums: number[], k: number): number {
const m = Math.max(...nums) + k * 2 + 2;
const d: number[] = new Array(m).fill(0);
const d: number[] = Array(m).fill(0);
for (const x of nums) {
d[x]++;
d[x + k * 2 + 1]--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tags:

<p><strong>解释:</strong></p>

<p>我们选择下标为 2 和 8 的元素,并且&nbsp;<code>1 * 4</code>&nbsp;是一个完全平方数。</p>
<p>我们选择下标为 2 和 8 的元素,并且&nbsp;<code>2 * 8</code>&nbsp;是一个完全平方数。</p>
</div>

<p><strong class="example">示例 2:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags:

<!-- problem:start -->

# [3173. 相邻元素的按位与 🔒](https://leetcode.cn/problems/bitwise-or-of-adjacent-elements)
# [3173. 相邻元素的按位或 🔒](https://leetcode.cn/problems/bitwise-or-of-adjacent-elements)

[English Version](/solution/3100-3199/3173.Bitwise%20OR%20of%20Adjacent%20Elements/README_EN.md)

Expand Down
Loading
Loading