Skip to content

[pull] main from doocs:main #31

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 3 commits into from
Aug 3, 2025
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
2 changes: 1 addition & 1 deletion solution/0400-0499/0427.Construct Quad Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Node {

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0427.Construct%20Quad%20Tree/images/new_top.png" style="height: 181px; width: 777px;" /></p>

<p>如果你想了解更多关于四叉树的内容,可以参考 <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a> 。</p>
<p>如果你想了解更多关于四叉树的内容,可以参考 <a href="https://baike.baidu.com/item/%E5%9B%9B%E5%8F%89%E6%A0%91">百科</a> 。</p>

<p><strong>四叉树格式:</strong></p>

Expand Down
2 changes: 1 addition & 1 deletion solution/0800-0899/0853.Car Fleet/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tags:

<p>There are <code>n</code> cars at given miles away from the starting mile 0, traveling to reach the mile <code>target</code>.</p>

<p>You are given two integer array <code>position</code> and <code>speed</code>, both of length <code>n</code>, where <code>position[i]</code> is the starting mile of the <code>i<sup>th</sup></code> car and <code>speed[i]</code> is the speed of the <code>i<sup>th</sup></code> car in miles per hour.</p>
<p>You are given two integer arrays&nbsp;<code>position</code> and <code>speed</code>, both of length <code>n</code>, where <code>position[i]</code> is the starting mile of the <code>i<sup>th</sup></code> car and <code>speed[i]</code> is the speed of the <code>i<sup>th</sup></code> car in miles per hour.</p>

<p>A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ Notice that node 4 is the last child of node 1.</pre>
<strong>Explanation:</strong> This example follows case 3 because node p is not in the sub-tree of node q and vice-versa. We can move node 3 with its sub-tree and make it as node 8&#39;s child.
</pre>

<p><strong class="example">Example 4:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1516.Move%20Sub-Tree%20of%20N-Ary%20Tree/images/untitled-diagramdrawio.png" style="width: 500px; height: 175px;" />
<pre>
<strong>Input:</strong> root = [1,null,2,3,null,4], p = 1, q = 4
<strong>Output:</strong> [4,null,1,null,2,3]
<strong>Explanation:</strong> This example follows case 1 because node q is in the sub-tree of node p. Disconnect 4 with its parent and move node 1 with its sub-tree and make it as node 4&#39;s child.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ tags:
<p>You are given a <strong>0-indexed</strong> integer array <code>piles</code>, where <code>piles[i]</code> represents the number of stones in the <code>i<sup>th</sup></code> pile, and an integer <code>k</code>. You should apply the following operation <strong>exactly</strong> <code>k</code> times:</p>

<ul>
<li>Choose any <code>piles[i]</code> and <strong>remove</strong> <code>ceil(piles[i] / 2)</code> stones from it.</li>
<li>Choose any <code>piles[i]</code> and <strong>remove</strong> <code>floor(piles[i] / 2)</code> stones from it.</li>
</ul>

<p><strong>Notice</strong> that you can apply the operation on the <strong>same</strong> pile more than once.</p>

<p>Return <em>the <strong>minimum</strong> possible total number of stones remaining after applying the </em><code>k</code><em> operations</em>.</p>

<p><code>ceil(x)</code> is the <b>smallest</b> integer that is <strong>greater</strong> than or <strong>equal</strong> to <code>x</code> (i.e., rounds <code>x</code> up).</p>
<p><code>floor(x)</code> is the <strong>largest</strong>&nbsp;integer that is <strong>smaller</strong> than or <strong>equal</strong> to <code>x</code> (i.e., rounds <code>x</code>&nbsp;down).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
3 changes: 3 additions & 0 deletions solution/1900-1999/1994.The Number of Good Subsets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ source: 第 60 场双周赛 Q4
tags:
- 位运算
- 数组
- 哈希表
- 数学
- 动态规划
- 状态压缩
- 计数
- 数论
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ source: Biweekly Contest 60 Q4
tags:
- Bit Manipulation
- Array
- Hash Table
- Math
- Dynamic Programming
- Bitmask
- Counting
- Number Theory
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ tags:

### 方法一:双指针

我们不妨假设移动的位置区间为 $[l,r]$,开始位置为 $startPos$,来看看如何算出移动的最小步数。根据 $startPos$ 所处的位置,我们可以分为三种情况:
我们不妨假设移动的位置区间为 $[l, r]$,开始位置为 $\textit{startPos}$,来看看如何算出移动的最小步数。根据 $\textit{startPos}$ 所处的位置,我们可以分为三种情况:

1. 如果 $startPos \leq l$,那么就是从 $startPos$ 一直向右移动到 $r$。移动的最小步数为 $r - startPos$;
2. 如果 $startPos \geq r$,那么就是从 $startPos$ 一直向左移动到 $l$。移动的最小步数为 $startPos - l$;
3. 如果 $l \lt startPos \lt r$,那么可以从 $startPos$ 向左移动到 $l$,再向右移动到 $r$;也可以从 $startPos$ 向右移动到 $r$,再向左移动到 $l$。移动的最小步数为 $r - l + \min(\lvert startPos - l \rvert, \lvert r - startPos \rvert)$。
1. 如果 $\textit{startPos} \leq l$,那么就是从 $\textit{startPos}$ 一直向右移动到 $r$。移动的最小步数为 $r - \textit{startPos}$;
2. 如果 $\textit{startPos} \geq r$,那么就是从 $\textit{startPos}$ 一直向左移动到 $l$。移动的最小步数为 $\textit{startPos} - l$;
3. 如果 $l < \textit{startPos} < r$,那么可以从 $\textit{startPos}$ 向左移动到 $l$,再向右移动到 $r$;也可以从 $\textit{startPos}$ 向右移动到 $r$,再向左移动到 $l$。移动的最小步数为 $r - l + \min(\lvert \textit{startPos} - l \rvert, \lvert r - \textit{startPos} \rvert)$。

以上三种情况可以统一用式子 $r - l + \min(\lvert startPos - l \rvert, \lvert r - startPos \rvert)$ 表示。
以上三种情况可以统一用式子 $r - l + \min(\lvert \textit{startPos} - l \rvert, \lvert r - \textit{startPos} \rvert)$ 表示。

假设我们固定区间右端点 $r$,向右移动左端点 $l$,我们来看看最小移动步数是怎么变化的。

1. 如果 $startPos \leq l$,随着 $l$ 的增大,最小步数不会发生变化。
2. 如果 $startPos \gt l$,随着 $l$ 的增大,最小步数会减小。
1. 如果 $\textit{startPos} \leq l$,随着 $l$ 的增大,最小步数不会发生变化。
2. 如果 $\textit{startPos} > l$,随着 $l$ 的增大,最小步数会减小。

因此,随着 $l$ 的增大,最小移动步数一定是非严格单调递减的。基于此,我们可以使用双指针的方法,找出所有符合条件的最大区间,然后取所有符合条件的区间中水果总数最大的一个作为答案。

具体地,我们用两个指针 $i$ 和 $j$ 分别指向区间的左右下标,初始时 $i = j = 0$。另外用一个变量 $s$ 记录区间内的水果总数,初始时 $s = 0$。

每次我们将 $j$ 加入区间中,然后更新 $s = s + fruits[j][1]$。如果此时区间内的最小步数 $fruits[j][0] - fruits[i][0] + \min(\lvert startPos - fruits[i][0] \rvert, \lvert startPos - fruits[j][0] \rvert)$ 大于 $k$,那么我们就将 $i$ 循环向右移动,直到 $i \gt j$ 或者区间内的最小步数小于等于 $k$。此时我们更新答案 $ans = \max(ans, s)$。继续移动 $j$,直到 $j$ 到达数组末尾。
每次我们将 $j$ 加入区间中,然后更新 $s = s + \textit{fruits}[j][1]$。如果此时区间内的最小步数 $\textit{fruits}[j][0] - \textit{fruits}[i][0] + \min(\lvert \textit{startPos} - \textit{fruits}[i][0] \rvert, \lvert \textit{startPos} - \textit{fruits}[j][0] \rvert)$ 大于 $k$,那么我们就将 $i$ 循环向右移动,直到 $i > j$ 或者区间内的最小步数小于等于 $k$。此时我们更新答案 $\textit{ans} = \max(\textit{ans}, s)$。继续移动 $j$,直到 $j$ 到达数组末尾。

最后返回答案即可。

Expand Down Expand Up @@ -219,6 +219,29 @@ function maxTotalFruits(fruits: number[][], startPos: number, k: number): number
}
```

#### Rust

```rust
impl Solution {
pub fn max_total_fruits(fruits: Vec<Vec<i32>>, start_pos: i32, k: i32) -> i32 {
let mut ans = 0;
let mut s = 0;
let mut i = 0;
for j in 0..fruits.len() {
let pj = fruits[j][0];
let fj = fruits[j][1];
s += fj;
while i <= j && pj - fruits[i][0] + std::cmp::min((start_pos - fruits[i][0]).abs(), (start_pos - pj).abs()) > k {
s -= fruits[i][1];
i += 1;
}
ans = ans.max(s)
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
<strong>Output:</strong> 9
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
Expand All @@ -45,7 +45,7 @@ You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
<pre>
<strong>Input:</strong> fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
<strong>Output:</strong> 14
<strong>Explanation:</strong>
<strong>Explanation:</strong>
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
Expand Down Expand Up @@ -82,7 +82,30 @@ You can move at most k = 2 steps and cannot reach any position with fruits.

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

Let's assume the movement range is $[l, r]$ and the starting position is $\textit{startPos}$. We need to calculate the minimum number of steps required. Based on the position of $\textit{startPos}$, we can divide this into three cases:

1. If $\textit{startPos} \leq l$, then we move right from $\textit{startPos}$ to $r$. The minimum number of steps is $r - \textit{startPos}$;
2. If $\textit{startPos} \geq r$, then we move left from $\textit{startPos}$ to $l$. The minimum number of steps is $\textit{startPos} - l$;
3. If $l < \textit{startPos} < r$, we can either move left from $\textit{startPos}$ to $l$ then right to $r$, or move right from $\textit{startPos}$ to $r$ then left to $l$. The minimum number of steps is $r - l + \min(\lvert \textit{startPos} - l \rvert, \lvert r - \textit{startPos} \rvert)$.

All three cases can be unified by the formula $r - l + \min(\lvert \textit{startPos} - l \rvert, \lvert r - \textit{startPos} \rvert)$.

Suppose we fix the right endpoint $r$ of the interval and move the left endpoint $l$ to the right. Let's see how the minimum number of steps changes:

1. If $\textit{startPos} \leq l$, as $l$ increases, the minimum number of steps remains unchanged.
2. If $\textit{startPos} > l$, as $l$ increases, the minimum number of steps decreases.

Therefore, as $l$ increases, the minimum number of steps is non-strictly monotonically decreasing. Based on this, we can use the two-pointer approach to find all qualifying maximum intervals, then take the one with the maximum total fruits among all qualifying intervals as the answer.

Specifically, we use two pointers $i$ and $j$ to point to the left and right indices of the interval, initially $i = j = 0$. We also use a variable $s$ to record the total number of fruits in the interval, initially $s = 0$.

Each time we include $j$ in the interval, then update $s = s + \textit{fruits}[j][1]$. If the minimum number of steps in the current interval $\textit{fruits}[j][0] - \textit{fruits}[i][0] + \min(\lvert \textit{startPos} - \textit{fruits}[i][0] \rvert, \lvert \textit{startPos} - \textit{fruits}[j][0] \rvert)$ is greater than $k$, we move $i$ to the right in a loop until $i > j$ or the minimum number of steps in the interval is less than or equal to $k$. At this point, we update the answer $\textit{ans} = \max(\textit{ans}, s)$. Continue moving $j$ until $j$ reaches the end of the array.

Finally, return the answer.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -197,6 +220,29 @@ function maxTotalFruits(fruits: number[][], startPos: number, k: number): number
}
```

#### Rust

```rust
impl Solution {
pub fn max_total_fruits(fruits: Vec<Vec<i32>>, start_pos: i32, k: i32) -> i32 {
let mut ans = 0;
let mut s = 0;
let mut i = 0;
for j in 0..fruits.len() {
let pj = fruits[j][0];
let fj = fruits[j][1];
s += fj;
while i <= j && pj - fruits[i][0] + std::cmp::min((start_pos - fruits[i][0]).abs(), (start_pos - pj).abs()) > k {
s -= fruits[i][1];
i += 1;
}
ans = ans.max(s)
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn max_total_fruits(fruits: Vec<Vec<i32>>, start_pos: i32, k: i32) -> i32 {
let mut ans = 0;
let mut s = 0;
let mut i = 0;
for j in 0..fruits.len() {
let pj = fruits[j][0];
let fj = fruits[j][1];
s += fj;
while i <= j
&& pj - fruits[i][0]
+ std::cmp::min((start_pos - fruits[i][0]).abs(), (start_pos - pj).abs())
> k
{
s -= fruits[i][1];
i += 1;
}
ans = ans.max(s)
}
ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ session_type is an ENUM (category) type of (Viewer, Streamer).
This table contains user id, session start, session end, session id and session type.
</pre>

<p>Write a solution to find the the <strong>users</strong> who have had <strong>at least one</strong> <strong>consecutive session</strong> of the <strong>same</strong> type (either &#39;<strong>Viewer</strong>&#39; or &#39;<strong>Streamer</strong>&#39;) with a <strong>maximum</strong> gap of <code>12</code> hours <strong>between</strong> sessions.</p>
<p>Write a solution to find the the <strong>users</strong> who have had <strong>at least two</strong><strong>&nbsp;session</strong> of the <strong>same</strong> type (either &#39;<strong>Viewer</strong>&#39; or &#39;<strong>Streamer</strong>&#39;) with a <strong>maximum</strong> gap of <code>12</code> hours <strong>between</strong> sessions.</p>

<p>Return <em>the result table ordered by </em><code>user_id</code><em> in <b>ascending</b> order.</em></p>

Expand Down Expand Up @@ -68,7 +68,7 @@ Sessions table:
| 103 |
+---------+
<strong>Explanation:</strong>
- User ID 101 will not be included in the final output as they do not have any consecutive sessions of the same session type.
- User ID 101 will not be included in the final output as they do not have any two sessions of the same session type.
- User ID 102 will be included in the final output as they had two viewer sessions with session IDs 3 and 4, respectively, and the time gap between them was less than 12 hours.
- User ID 103 participated in two viewer sessions with a gap of less than 12 hours between them, identified by session IDs 10 and 11. Therefore, user 103 will be included in the final output.
Output table is ordered by user_id in increasing order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
这张表的每一行包含任务标识,员工标识和每个任务的开始和结束时间。
</pre>

<p>编写一个解决方案来查找 <strong>每个</strong> 员工的任务 <strong>总持续时间</strong> 以及员工在任何时间点处理的 <strong>最大并发任务数</strong>。总时长应该 <strong>舍入</strong> 到最近的 <strong>整小时</strong>。</p>
<p>编写一个解决方案来查找 <strong>每个</strong> 员工的任务 <strong>总持续时间</strong> 以及员工在任何时间点处理的 <strong>最大并发任务数</strong>。总时长应该 <strong>向下取整</strong>&nbsp;到最近的 <strong>整小时</strong>。</p>

<p>返回结果表以&nbsp;<code>employee_id</code><strong> <em>升序</em></strong><em>&nbsp;排序。</em></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ product_id is the unique key for this table.
Each row of this table contains the ID and name of a product.
</pre>

<p>Write a solution to find all <strong>products</strong> whose names contain a <strong>sequence of exactly three digits in a row</strong>.&nbsp;</p>
<p>Write a solution to find all <strong>products</strong> whose names contain a <strong>sequence of exactly three consecutive digits in a row</strong>.&nbsp;</p>

<p>Return <em>the result table ordered by</em> <code>product_id</code> <em>in <strong>ascending</strong> order.</em></p>

<p>The result format is in the following example.</p>

<p><strong>Note</strong> that the name may contain multiple such sequences, but each should have length three.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3572.Maximize%20Y%E2%80%91Sum%20by%20Picking%20a%20Triplet%20of%20Distinct%20X%E2%80%91Values/README.md
rating: 1319
source: 第 158 场双周赛 Q1
tags:
- 贪心
- 数组
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3572.Maximize%20Y%E2%80%91Sum%20by%20Picking%20a%20Triplet%20of%20Distinct%20X%E2%80%91Values/README_EN.md
rating: 1319
source: Biweekly Contest 158 Q1
tags:
- Greedy
- Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3573.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20V/README.md
rating: 1777
source: 第 158 场双周赛 Q2
tags:
- 数组
- 动态规划
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3573.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20V/README_EN.md
rating: 1777
source: Biweekly Contest 158 Q2
tags:
- Array
- Dynamic Programming
Expand Down
2 changes: 2 additions & 0 deletions solution/3500-3599/3574.Maximize Subarray GCD Score/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3574.Maximize%20Subarray%20GCD%20Score/README.md
rating: 2257
source: 第 158 场双周赛 Q3
tags:
- 数组
- 数学
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3574.Maximize%20Subarray%20GCD%20Score/README_EN.md
rating: 2257
source: Biweekly Contest 158 Q3
tags:
- Array
- Math
Expand Down
2 changes: 2 additions & 0 deletions solution/3500-3599/3575.Maximum Good Subtree Score/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3575.Maximum%20Good%20Subtree%20Score/README.md
rating: 2359
source: 第 158 场双周赛 Q4
tags:
- 位运算
- 树
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3575.Maximum%20Good%20Subtree%20Score/README_EN.md
rating: 2359
source: Biweekly Contest 158 Q4
tags:
- Bit Manipulation
- Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3576.Transform%20Array%20to%20All%20Equal%20Elements/README.md
rating: 1489
source: 第 453 场周赛 Q1
tags:
- 贪心
- 数组
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3576.Transform%20Array%20to%20All%20Equal%20Elements/README_EN.md
rating: 1489
source: Weekly Contest 453 Q1
tags:
- Greedy
- Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3577.Count%20the%20Number%20of%20Computer%20Unlocking%20Permutations/README.md
rating: 1749
source: 第 453 场周赛 Q2
tags:
- 脑筋急转弯
- 数组
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3577.Count%20the%20Number%20of%20Computer%20Unlocking%20Permutations/README_EN.md
rating: 1749
source: Weekly Contest 453 Q2
tags:
- Brainteaser
- Array
Expand Down
Loading
Loading