Skip to content

Commit 312c25f

Browse files
Merge branch 'main' into main
2 parents bd1e1d9 + 5920db5 commit 312c25f

File tree

186 files changed

+5884
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+5884
-59
lines changed

solution/0000-0099/0098.Validate Binary Search Tree/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ tags:
2424
<p><strong>有效</strong> 二叉搜索树定义如下:</p>
2525

2626
<ul>
27-
<li>节点的左<span data-keyword="subtree">子树</span>只包含<strong> 小于 </strong>当前节点的数。</li>
28-
<li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>
27+
<li>节点的左<span data-keyword="subtree">子树</span>只包含<strong>&nbsp;严格小于 </strong>当前节点的数。</li>
28+
<li>节点的右子树只包含 <strong>严格大于</strong> 当前节点的数。</li>
2929
<li>所有左子树和右子树自身必须也是二叉搜索树。</li>
3030
</ul>
3131

solution/0000-0099/0098.Validate Binary Search Tree/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ tags:
2424
<p>A <strong>valid BST</strong> is defined as follows:</p>
2525

2626
<ul>
27-
<li>The left <span data-keyword="subtree">subtree</span> of a node contains only nodes with keys <strong>less than</strong> the node&#39;s key.</li>
28-
<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node&#39;s key.</li>
27+
<li>The left <span data-keyword="subtree">subtree</span> of a node contains only nodes with keys&nbsp;<strong>strictly less than</strong> the node&#39;s key.</li>
28+
<li>The right subtree of a node contains only nodes with keys <strong>strictly greater than</strong> the node&#39;s key.</li>
2929
<li>Both the left and right subtrees must also be binary search trees.</li>
3030
</ul>
3131

solution/0100-0199/0135.Candy/README.md

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

2424
<ul>
2525
<li>每个孩子至少分配到 <code>1</code> 个糖果。</li>
26-
<li>相邻两个孩子评分更高的孩子会获得更多的糖果。</li>
26+
<li>相邻两个孩子中,评分更高的那个会获得更多的糖果。</li>
2727
</ul>
2828

2929
<p>请你给每个孩子分发糖果,计算并返回需要准备的 <strong>最少糖果数目</strong> 。</p>

solution/0400-0499/0427.Construct Quad Tree/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Node {
5050

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

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

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

solution/0400-0499/0472.Concatenated Words/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tags:
88
- 数组
99
- 字符串
1010
- 动态规划
11+
- 排序
1112
---
1213

1314
<!-- problem:start -->

solution/0400-0499/0472.Concatenated Words/README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tags:
88
- Array
99
- String
1010
- Dynamic Programming
11+
- Sorting
1112
---
1213

1314
<!-- problem:start -->

solution/0400-0499/0499.The Maze III/README_EN.md

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

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

26-
<p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won&#39;t stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.</p>
26+
<p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won&#39;t stop rolling until hitting a wall. When the ball stops, it could choose the next direction (must be different from last chosen direction). There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.</p>
2727

2828
<p>Given the <code>m x n</code> <code>maze</code>, the ball&#39;s position <code>ball</code> and the hole&#39;s position <code>hole</code>, where <code>ball = [ball<sub>row</sub>, ball<sub>col</sub>]</code> and <code>hole = [hole<sub>row</sub>, hole<sub>col</sub>]</code>, return <em>a string </em><code>instructions</code><em> of all the instructions that the ball should follow to drop in the hole with the <strong>shortest distance</strong> possible</em>. If there are multiple valid instructions, return the <strong>lexicographically minimum</strong> one. If the ball can&#39;t drop in the hole, return <code>&quot;impossible&quot;</code>.</p>
2929

solution/0800-0899/0853.Car Fleet/README_EN.md

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

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

24-
<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>
24+
<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>
2525

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

solution/0900-0999/0904.Fruit Into Baskets/README.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ tags:
8383

8484
### 方法一:哈希表 + 滑动窗口
8585

86-
我们用哈希表 $cnt$ 维护当前窗口内的水果种类以及对应的数量,用双指针 $j$ 和 $i$ 维护窗口的左右边界。
86+
我们用哈希表 $\textit{cnt}$ 维护当前窗口内的水果种类以及对应的数量,用双指针 $j$ 和 $i$ 维护窗口的左右边界。
8787

88-
遍历数组 `fruits`,将当前水果 $x$ 加入窗口,即 $cnt[x]++$,然后判断当前窗口内的水果种类是否超过了 $2$ 种,如果超过了 $2$ 种,就需要将窗口的左边界 $j$ 右移,直到窗口内的水果种类不超过 $2$ 种为止。然后更新答案,即 $ans = \max(ans, i - j + 1)$。
88+
遍历数组 $\textit{fruits}$,将当前水果 $x$ 加入窗口,即 $\textit{cnt}[x]++$,然后判断当前窗口内的水果种类是否超过了 $2$ 种,如果超过了 $2$ 种,就需要将窗口的左边界 $j$ 右移,直到窗口内的水果种类不超过 $2$ 种为止。然后更新答案,即 $\textit{ans} = \max(\textit{ans}, i - j + 1)$。
8989

9090
遍历结束后,即可得到最终的答案。
9191

@@ -105,7 +105,7 @@ j i
105105
j i
106106
```
107107

108-
时间复杂度 $O(n)$,其中 $n$ 为数组 `fruits` 的长度。空间复杂度 $O(1)$。
108+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{fruits}$ 的长度。空间复杂度 $O(1)$,因为哈希表 $\textit{cnt}$ 中的键值对数量最多为 $2$。
109109

110110
<!-- tabs:start -->
111111

@@ -248,19 +248,49 @@ impl Solution {
248248
}
249249
```
250250

251+
#### C#
252+
253+
```cs
254+
public class Solution {
255+
public int TotalFruit(int[] fruits) {
256+
var cnt = new Dictionary<int, int>();
257+
int ans = 0;
258+
for (int i = 0, j = 0; i < fruits.Length; ++i) {
259+
int x = fruits[i];
260+
if (cnt.ContainsKey(x)) {
261+
cnt[x]++;
262+
} else {
263+
cnt[x] = 1;
264+
}
265+
while (cnt.Count > 2) {
266+
int y = fruits[j++];
267+
if (cnt.ContainsKey(y)) {
268+
cnt[y]--;
269+
if (cnt[y] == 0) {
270+
cnt.Remove(y);
271+
}
272+
}
273+
}
274+
ans = Math.Max(ans, i - j + 1);
275+
}
276+
return ans;
277+
}
278+
}
279+
```
280+
251281
<!-- tabs:end -->
252282

253283
<!-- solution:end -->
254284

255285
<!-- solution:start -->
256286

257-
### 方法二:滑动窗口优化
287+
### 方法二:单调变长滑动窗口
258288

259289
在方法一中,我们发现,窗口大小会时而变大,时而变小,这就需要我们每一次更新答案。
260290

261291
但本题实际上求的是水果的最大数目,也就是“最大”的窗口,我们没有必要缩小窗口,只需要让窗口单调增大。于是代码就少了每次更新答案的操作,只需要在遍历结束后将此时的窗口大小作为答案返回即可。
262292

263-
时间复杂度 $O(n)$,其中 $n$ 为数组 `fruits` 的长度。空间复杂度 $O(1)$
293+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{fruits}$ 的长度。
264294

265295
<!-- tabs:start -->
266296

@@ -395,6 +425,35 @@ impl Solution {
395425
}
396426
```
397427

428+
#### C#
429+
430+
```cs
431+
public class Solution {
432+
public int TotalFruit(int[] fruits) {
433+
var cnt = new Dictionary<int, int>();
434+
int j = 0, n = fruits.Length;
435+
foreach (int x in fruits) {
436+
if (cnt.ContainsKey(x)) {
437+
cnt[x]++;
438+
} else {
439+
cnt[x] = 1;
440+
}
441+
442+
if (cnt.Count > 2) {
443+
int y = fruits[j++];
444+
if (cnt.ContainsKey(y)) {
445+
cnt[y]--;
446+
if (cnt[y] == 0) {
447+
cnt.Remove(y);
448+
}
449+
}
450+
}
451+
}
452+
return n - j;
453+
}
454+
}
455+
```
456+
398457
<!-- tabs:end -->
399458

400459
<!-- solution:end -->

solution/0900-0999/0904.Fruit Into Baskets/README_EN.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ If we had started at the first tree, we would only pick from trees [1,2].
7575

7676
We use a hash table $cnt$ to maintain the types and corresponding quantities of fruits in the current window, and use two pointers $j$ and $i$ to maintain the left and right boundaries of the window.
7777

78-
We traverse the `fruits` array, add the current fruit $x$ to the window, i.e., $cnt[x]++$, then judge whether the types of fruits in the current window exceed $2$. If it exceeds $2$, we need to move the left boundary $j$ of the window to the right until the types of fruits in the window do not exceed $2$. Then we update the answer, i.e., $ans = \max(ans, i - j + 1)$.
78+
We traverse the $\textit{fruits}$ array, add the current fruit $x$ to the window, i.e., $cnt[x]++$, then judge whether the types of fruits in the current window exceed $2$. If it exceeds $2$, we need to move the left boundary $j$ of the window to the right until the types of fruits in the window do not exceed $2$. Then we update the answer, i.e., $ans = \max(ans, i - j + 1)$.
7979

8080
After the traversal ends, we can get the final answer.
8181

@@ -95,7 +95,7 @@ j i
9595
j i
9696
```
9797

98-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the `fruits` array.
98+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the $\textit{fruits}$ array.
9999

100100
<!-- tabs:start -->
101101

@@ -238,19 +238,49 @@ impl Solution {
238238
}
239239
```
240240

241+
#### C#
242+
243+
```cs
244+
public class Solution {
245+
public int TotalFruit(int[] fruits) {
246+
var cnt = new Dictionary<int, int>();
247+
int ans = 0;
248+
for (int i = 0, j = 0; i < fruits.Length; ++i) {
249+
int x = fruits[i];
250+
if (cnt.ContainsKey(x)) {
251+
cnt[x]++;
252+
} else {
253+
cnt[x] = 1;
254+
}
255+
while (cnt.Count > 2) {
256+
int y = fruits[j++];
257+
if (cnt.ContainsKey(y)) {
258+
cnt[y]--;
259+
if (cnt[y] == 0) {
260+
cnt.Remove(y);
261+
}
262+
}
263+
}
264+
ans = Math.Max(ans, i - j + 1);
265+
}
266+
return ans;
267+
}
268+
}
269+
```
270+
241271
<!-- tabs:end -->
242272

243273
<!-- solution:end -->
244274

245275
<!-- solution:start -->
246276

247-
### Solution 2: Sliding Window Optimization
277+
### Solution 2: Monotonic Variable-Length Sliding Window
248278

249279
In Solution 1, we find that the window size sometimes increases and sometimes decreases, which requires us to update the answer each time.
250280

251281
But what this problem actually asks for is the maximum number of fruits, that is, the "largest" window. We don't need to shrink the window, we just need to let the window monotonically increase. So the code omits the operation of updating the answer each time, and only needs to return the size of the window as the answer after the traversal ends.
252282

253-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the `fruits` array.
283+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $\textit{fruits}$ array.
254284

255285
<!-- tabs:start -->
256286

@@ -385,6 +415,35 @@ impl Solution {
385415
}
386416
```
387417

418+
#### C#
419+
420+
```cs
421+
public class Solution {
422+
public int TotalFruit(int[] fruits) {
423+
var cnt = new Dictionary<int, int>();
424+
int j = 0, n = fruits.Length;
425+
foreach (int x in fruits) {
426+
if (cnt.ContainsKey(x)) {
427+
cnt[x]++;
428+
} else {
429+
cnt[x] = 1;
430+
}
431+
432+
if (cnt.Count > 2) {
433+
int y = fruits[j++];
434+
if (cnt.ContainsKey(y)) {
435+
cnt[y]--;
436+
if (cnt[y] == 0) {
437+
cnt.Remove(y);
438+
}
439+
}
440+
}
441+
}
442+
return n - j;
443+
}
444+
}
445+
```
446+
388447
<!-- tabs:end -->
389448

390449
<!-- solution:end -->

0 commit comments

Comments
 (0)