Skip to content

feat: update lc problems #2099

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
Dec 14, 2023
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/0000-0099/0038.Count and Say/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"

**方法一: 模拟**

题目要求输出第 $n$ 项的外观序列,而第 $n$ 项是序列中第 $n-1$ 项的描述。所以我们遍历 $n-1$ 次,每次迭代用快慢指针j和i,分别记录当前字符的位置以及下一个不等于当前字符的位置,更新上一项的序列为 $j-i$ 个当前字符。
题目要求输出第 $n$ 项的外观序列,而第 $n$ 项是序列中第 $n-1$ 项的描述。所以我们遍历 $n-1$ 次,每次迭代用快慢指针 j 和 i,分别记录当前字符的位置以及下一个不等于当前字符的位置,更新上一项的序列为 $j-i$ 个当前字符。

时间复杂度:

Expand Down
2 changes: 1 addition & 1 deletion solution/0200-0299/0274.H-Index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p>给你一个整数数组 <code>citations</code> ,其中 <code>citations[i]</code> 表示研究者的第 <code>i</code> 篇论文被引用的次数。计算并返回该研究者的 <strong><code>h</code><em>&nbsp;</em>指数</strong>。</p>

<p>根据维基百科上&nbsp;<a href="https://baike.baidu.com/item/h-index/3991452?fr=aladdin" target="_blank">h 指数的定义</a>:<code>h</code> 代表“高引用次数” ,一名科研人员的 <code>h</code><strong> 指数 </strong>是指他(她)至少发表了 <code>h</code> 篇论文,并且每篇论文<strong> 至少</strong> 被引用 <code>h</code> 。如果 <code>h</code><em> </em>有多种可能的值,<strong><code>h</code> 指数 </strong>是其中最大的那个。</p>
<p>根据维基百科上&nbsp;<a href="https://baike.baidu.com/item/h-index/3991452?fr=aladdin" target="_blank">h 指数的定义</a>:<code>h</code> 代表“高引用次数” ,一名科研人员的 <code>h</code><strong> 指数 </strong>是指他(她)至少发表了 <code>h</code> 篇论文,并且&nbsp;<strong>至少&nbsp;</strong>有 <code>h</code> 篇论文被引用次数大于等于 <code>h</code> 。如果 <code>h</code><em> </em>有多种可能的值,<strong><code>h</code> 指数 </strong>是其中最大的那个。</p>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<li><code>-10<sup>9</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
<li><code>nums1</code> and <code>nums2</code> both are sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
<li><code>k &lt;=&nbsp;nums1.length *&nbsp;nums2.length</code></li>
</ul>

## Solutions
Expand Down
2 changes: 1 addition & 1 deletion solution/0500-0599/0511.Game Play Analysis I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<p>&nbsp;</p>

<p>查询每位玩家 <strong>第一次登陆平台的日期</strong>。</p>
<p>查询每位玩家 <strong>第一次登录平台的日期</strong>。</p>

<p>查询结果的格式如下所示:</p>

Expand Down
4 changes: 2 additions & 2 deletions solution/0500-0599/0598.Range Addition II/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# [598. 范围求和 II](https://leetcode.cn/problems/range-addition-ii)
# [598. 区间加法 II](https://leetcode.cn/problems/range-addition-ii)

[English Version](/solution/0500-0599/0598.Range%20Addition%20II/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>给你一个 <code>m x&nbsp;n</code> 的矩阵&nbsp;<code>M</code><strong>&nbsp;</strong>,初始化时所有的 <code>0</code> 和一个操作数组 <code>op</code> ,其中 <code>ops[i] = [ai, bi]</code> 意味着当所有的 <code>0 &lt;= x &lt; ai</code> 和 <code>0 &lt;= y &lt; bi</code> 时, <code>M[x][y]</code> 应该加 1。</p>
<p>给你一个 <code>m x&nbsp;n</code> 的矩阵&nbsp;<code>M</code><strong> </strong>和一个操作数组 <code>op</code> 。矩阵初始化时所有的单元格都为 <code>0</code> <code>ops[i] = [ai, bi]</code> 意味着当所有的 <code>0 &lt;= x &lt; ai</code> 和 <code>0 &lt;= y &lt; bi</code> 时, <code>M[x][y]</code> 应该加 1。</p>

<p>在&nbsp;<em>执行完所有操作后</em>&nbsp;,计算并返回&nbsp;<em>矩阵中最大整数的个数</em>&nbsp;。</p>

Expand Down
60 changes: 18 additions & 42 deletions solution/0600-0699/0663.Equal Tree Partition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,33 @@

<!-- 这里写题目描述 -->

<p>给定一棵有 <code>n</code> 个结点的二叉树,你的任务是检查是否可以通过去掉树上的一条边将树分成两棵,且这两棵树结点之和相等。</p>

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

<pre><strong>输入:</strong>
5
/ \
10 10
/ \
2 3

<strong>输出:</strong> True
<strong>解释:</strong>
5
/
10

和: 15

10
/ \
2 3

和: 15
</pre>
<p>给你一棵二叉树的根节点 <code>root</code>,如果你可以通过去掉原始树上的一条边将树分成两棵节点值之和相等的子树,则返回 <code>true</code>。</p>

<p>&nbsp;</p>

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

<pre><strong>输入:</strong>
1
/ \
2 10
/ \
2 20
<p><strong class="example">示例 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0663.Equal%20Tree%20Partition/images/split1-tree.jpg" style="width: 500px; height: 204px;" />
<pre>
<strong>输入:</strong>root = [5,10,10,null,null,2,3]
<strong>输出:</strong>true
</pre>

<strong>输出:</strong> False
<strong>解释:</strong> 无法通过移除一条树边将这棵树划分成结点之和相等的两棵子树。
<p><strong class="example">示例 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0600-0699/0663.Equal%20Tree%20Partition/images/split2-tree.jpg" style="width: 277px; height: 302px;" />
<pre>
<strong>输入:</strong>root = [1,2,10,null,null,2,20]
<strong>输出:</strong>false
<strong>解释:</strong>在树上移除一条边无法将树分成两棵节点值之和相等的子树。
</pre>

<p>&nbsp;</p>

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

<ol>
<li>树上结点的权值范围 [-100000, 100000]。</li>
<li>1 &lt;= n &lt;= 10000</li>
</ol>

<p>&nbsp;</p>
<ul>
<li>树中节点数目在 <code>[1, 10<sup>4</sup>]</code> 范围内。</li>
<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

## 解法

Expand Down
4 changes: 2 additions & 2 deletions solution/0800-0899/0840.Magic Squares In Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<p><strong>示例 2:</strong></p>

<pre>
<strong>输出:</strong> grid = [[8]]
<strong>输入:</strong> 0
<strong>输入:</strong> grid = [[8]]
<strong>输出:</strong> 0
</pre>

<p>&nbsp;</p>
Expand Down
5 changes: 2 additions & 3 deletions solution/1200-1299/1206.Design Skiplist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

<p>例如,一个跳表包含 <code>[30, 40, 50, 60, 70, 90]</code> ,然后增加 <code>80</code>、<code>45</code> 到跳表中,以下图的方式操作:</p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1206.Design%20Skiplist/images/1506_skiplist.gif" /><br />
<small>Artyom Kalinin [CC BY-SA 3.0], via <a href="https://commons.wikimedia.org/wiki/File:Skip_list_add_element-en.gif" target="_blank" title="Artyom Kalinin [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons">Wikimedia Commons</a></small></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1206.Design%20Skiplist/images/1702370216-mKQcTt-1506_skiplist.gif" style="width: 500px; height: 173px;" /></p>

<p>跳表中有很多层,每一层是一个短的链表。在第一层的作用下,增加、删除和搜索操作的时间复杂度不超过 <code>O(n)</code>。跳表的每一个操作的平均时间复杂度是 <code>O(log(n))</code>,空间复杂度是 <code>O(n)</code>。</p>

<p>了解更多 :&nbsp;<a href="https://en.wikipedia.org/wiki/Skip_list" target="_blank">https://en.wikipedia.org/wiki/Skip_list</a></p>
<p>了解更多 :&nbsp;<a href="https://oi-wiki.org/ds/skiplist/" target="_blank">https://oi-wiki.org/ds/skiplist/</a></p>

<p>在本题中,你的设计应该要包含这些函数:</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Description

<p>On a <strong>0-indexed</strong> <code>8 x 8</code> chessboard, there can be multiple black queens ad one white king.</p>
<p>On a <strong>0-indexed</strong> <code>8 x 8</code> chessboard, there can be multiple black queens and one white king.</p>

<p>You are given a 2D integer array <code>queens</code> where <code>queens[i] = [xQueen<sub>i</sub>, yQueen<sub>i</sub>]</code> represents the position of the <code>i<sup>th</sup></code> black queen on the chessboard. You are also given an integer array <code>king</code> of length <code>2</code> where <code>king = [xKing, yKing]</code> represents the position of the white king.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</li>
<li>The size of each block of consecutive <code>0</code>&#39;s is a multiple of <code>zeroGroup</code>.
<ul>
<li>For example, in a binary string <code><u>00</u>11<u>0</u>1111<u>00</u></code> sizes of each block of consecutive ones are <code>[2,1,2]</code>.</li>
<li>For example, in a binary string <code><u>00</u>11<u>0</u>1111<u>00</u></code> sizes of each block of consecutive zeros are <code>[2,1,2]</code>.</li>
</ul>
</li>
</ul>
Expand Down
1 change: 1 addition & 0 deletions solution/2600-2699/2692.Make Object Immutable/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn = (obj) =&gt; {
<p><strong>Constraints:</strong></p>

<ul>
<li><code>obj</code>&nbsp;is a valid JSON object or array</li>
<li><code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>
</ul>

Expand Down
22 changes: 11 additions & 11 deletions solution/2700-2799/2715.Timeout Cancellation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<p>Given a function <code>fn</code>, an array of&nbsp;arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>

<p>After a delay of <code>cancelT</code>, the returned cancel function <code>cancelFn</code> will be invoked.</p>
<p>After a delay of <code>cancelTimeMs</code>, the returned cancel function <code>cancelFn</code> will be invoked.</p>

<pre>
setTimeout(cancelFn, cancelT)
setTimeout(cancelFn, cancelTimeMs)
</pre>

<p>Initially, the execution of the function <code>fn</code> should be delayed by <code>t</code> milliseconds.</p>
Expand All @@ -23,7 +23,7 @@ setTimeout(cancelFn, cancelT)
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
<strong>Explanation:</strong>
const cancelT = 50;
const cancelTimeMs = 50;
const result = [];

const fn = (x) =&gt; x * 5;
Expand All @@ -39,13 +39,13 @@ const cancel = cancellable(log, [2], 20);

const maxT = Math.max(t, 50);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
setTimeout(cancel, cancelT);
setTimeout(cancel, cancelTimeMs);

setTimeout(() =&gt; {
&nbsp; &nbsp; console.log(result); // [{&quot;time&quot;:20,&quot;returned&quot;:10}]
}, 65);
}, maxT + 15);

The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened after the execution of fn(2) at 20ms.
</pre>

<p><strong class="example">Example 2:</strong></p>
Expand All @@ -54,8 +54,8 @@ The cancellation was scheduled to occur after a delay of cancelT (50ms), which h
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100
<strong>Output:</strong> []
<strong>Explanation:</strong>
const cancelT = 50;
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
const cancelTimeMs = 50;
The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
</pre>

<p><strong class="example">Example 3:</strong></p>
Expand All @@ -64,8 +64,8 @@ The cancellation was scheduled to occur after a delay of cancelT (50ms), which h
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
<strong>Explanation:
</strong>const cancelT = 100;
The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
</strong>const cancelTimeMs = 100;
The cancellation was scheduled to occur after a delay of cancelTimeMs (100ms), which happened after the execution of fn(2,4) at 30ms.
</pre>

<p>&nbsp;</p>
Expand All @@ -76,7 +76,7 @@ The cancellation was scheduled to occur after a delay of cancelT (100ms), which
<li><code>args</code> is a valid JSON array</li>
<li><code>1 &lt;= args.length &lt;= 10</code></li>
<li><code><font face="monospace">20 &lt;= t &lt;= 1000</font></code></li>
<li><code><font face="monospace">10 &lt;= cancelT &lt;= 1000</font></code></li>
<li><code><font face="monospace">10 &lt;= cancelTimeMs &lt;= 1000</font></code></li>
</ul>

## Solutions
Expand Down
38 changes: 22 additions & 16 deletions solution/2700-2799/2725.Interval Cancellation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<p>Given a function <code>fn</code>, an array of arguments&nbsp;<code>args</code>, and&nbsp;an interval time <code>t</code>, return a cancel function <code>cancelFn</code>.</p>

<p>The function <code>fn</code> should be called with <code>args</code> immediately and then called again every&nbsp;<code>t</code> milliseconds&nbsp;until&nbsp;<code>cancelFn</code>&nbsp;is called at <code>cancelT</code> ms.</p>
<p>The function <code>fn</code> should be called with <code>args</code> immediately and then called again every&nbsp;<code>t</code> milliseconds&nbsp;until&nbsp;<code>cancelFn</code>&nbsp;is called at <code>cancelTimeMs</code> ms.</p>

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

<pre>
<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 35, cancelT = 190
<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 35
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 8},
Expand All @@ -23,23 +23,23 @@
{&quot;time&quot;: 175, &quot;returned&quot;: 8}
]
<strong>Explanation:</strong>
const result = []
const fn = (x) =&gt; x * 2
const args = [4], t = 35, cancelT = 190
const result = [];
const fn = (x) =&gt; x * 2;
const cancelTimeMs = 190;

const start = performance.now()
const start = performance.now();

const log = (...argsArr) =&gt; {
const diff = Math.floor(performance.now() - start)
result.push({&quot;time&quot;: diff, &quot;returned&quot;: fn(...argsArr)})
const diff = Math.floor(performance.now() - start);
result.push({&quot;time&quot;: diff, &quot;returned&quot;: fn(...argsArr)});
}

const cancel = cancellable(log, [4], 35);
setTimeout(cancel, 190);
setTimeout(cancel, cancelTimeMs);

setTimeout(() =&gt; {
console.log(result) // Output
}, cancelT + t + 15)
console.log(result); // Output
}, cancelTimeMs + 50)

Every 35ms, fn(4) is called. Until t=190ms, then it is cancelled.
1st fn call is at 0ms. fn(4) returns 8.
Expand All @@ -54,7 +54,7 @@ Cancelled at 190ms
<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 30, cancelT = 165
<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 30
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 10},
Expand All @@ -64,7 +64,10 @@ Cancelled at 190ms
{&quot;time&quot;: 120, &quot;returned&quot;: 10},
{&quot;time&quot;: 150, &quot;returned&quot;: 10}
]
<strong>Explanation:</strong> Every 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.
<strong>Explanation:</strong>
const cancelTimeMs = 165;

Every 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.
1st fn call is at 0ms&nbsp;
2nd fn call is at 30ms&nbsp;
3rd fn call is at 60ms&nbsp;
Expand All @@ -77,15 +80,18 @@ Cancelled at 165ms
<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180
<strong>Input:</strong> fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 9},
{&quot;time&quot;: 50, &quot;returned&quot;: 9},
{&quot;time&quot;: 100, &quot;returned&quot;: 9},
{&quot;time&quot;: 150, &quot;returned&quot;: 9}
]
<strong>Explanation:</strong> Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
<strong>Explanation:</strong>
const cancelTimeMs = 180;

Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
1st fn call is at 0ms
2nd fn call is at 50ms
3rd fn call is at 100ms
Expand All @@ -101,7 +107,7 @@ Cancelled at 180ms
<li><code>args</code> is a valid JSON array</li>
<li><code>1 &lt;= args.length &lt;= 10</code></li>
<li><code><font face="monospace">30 &lt;= t &lt;= 100</font></code></li>
<li><code><font face="monospace">10 &lt;= cancelT &lt;= 500</font></code></li>
<li><code><font face="monospace">10 &lt;= </font>cancelTimeMs<font face="monospace"> &lt;= 500</font></code></li>
</ul>

## Solutions
Expand Down
10 changes: 5 additions & 5 deletions solution/2800-2899/2865.Beautiful Towers I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

<ol>
<li><code>1 &lt;= heights[i] &lt;= maxHeights[i]</code></li>
<li><code>heights</code>&nbsp;是一个 <strong>山状</strong>&nbsp;数组。</li>
<li><code>heights</code>&nbsp;是一个 <strong>山脉</strong> 数组。</li>
</ol>

<p>如果存在下标 <code>i</code>&nbsp;满足以下条件,那么我们称数组&nbsp;<code>heights</code>&nbsp;是一个 <strong>山状</strong>&nbsp;数组:</p>
<p>如果存在下标 <code>i</code>&nbsp;满足以下条件,那么我们称数组&nbsp;<code>heights</code>&nbsp;是一个 <strong>山脉</strong> 数组:</p>

<ul>
<li>对于所有&nbsp;<code>0 &lt; j &lt;= i</code>&nbsp;,都有&nbsp;<code>heights[j - 1] &lt;= heights[j]</code></li>
Expand All @@ -35,7 +35,7 @@
<b>输出:</b>13
<b>解释:</b>和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
- 1 &lt;= heights[i] &lt;= maxHeights[i]
- heights 是个山状数组,峰值在 i = 0 处。
- heights 是个山脉数组,峰值在 i = 0 处。
13 是所有美丽塔方案中的最大高度和。</pre>

<p><strong class="example">示例 2:</strong></p>
Expand All @@ -45,7 +45,7 @@
<b>输出:</b>22
<strong>解释:</strong> 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
- 1 &lt;= heights[i] &lt;= maxHeights[i]
- heights 是个山状数组,峰值在 i = 3 处。
- heights 是个山脉数组,峰值在 i = 3 处。
22 是所有美丽塔方案中的最大高度和。</pre>

<p><strong class="example">示例 3:</strong></p>
Expand All @@ -55,7 +55,7 @@
<b>输出:</b>18
<strong>解释:</strong>和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
- 1 &lt;= heights[i] &lt;= maxHeights[i]
- heights 是个山状数组,最大值在 i = 2 处。
- heights 是个山脉数组,最大值在 i = 2 处。
注意,在这个方案中,i = 3 也是一个峰值。
18 是所有美丽塔方案中的最大高度和。
</pre>
Expand Down
Loading