Skip to content

Commit

Permalink
chore: update lc problems (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
acbin authored Dec 18, 2023
1 parent 468e051 commit 3d74b5a
Show file tree
Hide file tree
Showing 31 changed files with 100 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Description

<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 &lt;= index<sub>1</sub> &lt; index<sub>2</sub> &lt;&nbsp;numbers.length</code>.</p>
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 &lt;= index<sub>1</sub> &lt; index<sub>2</sub> &lt;= numbers.length</code>.</p>

<p>Return<em> the indices of the two numbers, </em><code>index<sub>1</sub></code><em> and </em><code>index<sub>2</sub></code><em>, <strong>added by one</strong> as an integer array </em><code>[index<sub>1</sub>, index<sub>2</sub>]</code><em> of length 2.</em></p>

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

<p>You are given an array of strings <code>words</code> and a string <code>chars</code>.</p>

<p>A string is <strong>good</strong> if it can be formed by characters from chars (each character can only be used once).</p>
<p>A string is <strong>good</strong> if it can be formed by characters from <code>chars</code> (each character can only be used once).</p>

<p>Return <em>the sum of lengths of all good strings in words</em>.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<pre>
<b>输入:</b>nums = [1,7,9,2,5], lower = 11, upper = 11
<b>输出:</b>1
<b>解释:</b>只有单个公平数对:(2,3) 。
<b>解释:</b>只有单个公平数对:(2,9) 。
</pre>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ args = [{&quot;item&quot;: &quot;burger&quot;}, 10, 1.1]
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul style="list-style-type:square;">
<ul>
<li><code><font face="monospace">typeof args[0] == &#39;object&#39; and args[0] != null</font></code></li>
<li><code>1 &lt;= args.length &lt;= 100</code></li>
<li><code>2 &lt;= JSON.stringify(args[0]).length &lt;= 10<sup>5</sup></code></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

<p>Write a function that accepts two deeply nested objects or arrays&nbsp;<code>obj1</code> and&nbsp;<code>obj2</code>&nbsp;and returns a new&nbsp;object representing their differences.</p>

<p>The function should compare the properties of the two objects and identify any changes.&nbsp;The returned object should only contains keys where the value is different from&nbsp;<code>obj1</code> to&nbsp;<code>obj2</code>. For each changed key, the value should be represented as an&nbsp;array <code>[obj1 value, obj2&nbsp;value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys.&nbsp;The end result should be a deeply nested object where each leaf value is a difference array.</p>
<p>The function should compare the properties of the two objects and identify any changes.&nbsp;The returned object should only contains keys where the value is different from&nbsp;<code>obj1</code> to&nbsp;<code>obj2</code>.</p>

<p>For each changed key, the value should be represented as an&nbsp;array <code>[obj1 value, obj2&nbsp;value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys.&nbsp;The end result should be a deeply nested object where each leaf value is a difference array.</p>

<p>You may assume that both objects are the output of <code>JSON.parse</code>.</p>

Expand Down
28 changes: 8 additions & 20 deletions solution/2700-2799/2715.Timeout Cancellation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,8 @@ setTimeout(cancelFn, cancelTimeMs)
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
<strong>Explanation:</strong>
const cancelTimeMs = 50;
const result = [];

const fn = (x) =&gt; x * 5;

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)});
}
&nbsp; &nbsp;&nbsp;
const cancel = cancellable(log, [2], 20);

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

setTimeout(() =&gt; {
&nbsp; &nbsp; console.log(result); // [{&quot;time&quot;:20,&quot;returned&quot;:10}]
}, maxT + 15);
const cancelFn = cancellable((x) =&gt; x * 5, [2], 20);
setTimeout(cancelFn, cancelTimeMs);

The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened after the execution of fn(2) at 20ms.
</pre>
Expand All @@ -55,6 +37,9 @@ The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), wh
<strong>Output:</strong> []
<strong>Explanation:</strong>
const cancelTimeMs = 50;
const cancelFn = cancellable((x) =&gt; x**2, [2], 100);
setTimeout(cancelFn, cancelTimeMs);

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>

Expand All @@ -65,6 +50,9 @@ The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), wh
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
<strong>Explanation:
</strong>const cancelTimeMs = 100;
const cancelFn = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30);
setTimeout(cancelFn, cancelTimeMs);

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

Expand Down
4 changes: 2 additions & 2 deletions solution/2700-2799/2724.Sort By/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<p><strong>Constraints:</strong></p>

<ul>
<li><code>arr is a valid JSON array</code></li>
<li><code>fn is a function that returns a number</code></li>
<li><code>arr</code> is a valid JSON array</li>
<li><code>fn</code> is a function that returns a number</li>
<li><code>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>
</ul>

Expand Down
24 changes: 7 additions & 17 deletions solution/2700-2799/2725.Interval Cancellation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,9 @@
{&quot;time&quot;: 175, &quot;returned&quot;: 8}
]
<strong>Explanation:</strong>
const result = [];
const fn = (x) =&gt; x * 2;
const cancelTimeMs = 190;

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 cancel = cancellable(log, [4], 35);
setTimeout(cancel, cancelTimeMs);

setTimeout(() =&gt; {
console.log(result); // Output
}, cancelTimeMs + 50)
const cancelFn = cancellable((x) =&gt; x * 2, [4], 35);
setTimeout(cancelFn, cancelTimeMs);

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 @@ -65,7 +51,9 @@ Cancelled at 190ms
{&quot;time&quot;: 150, &quot;returned&quot;: 10}
]
<strong>Explanation:</strong>
const cancelTimeMs = 165;
const cancelTimeMs = 165;
const cancelFn = cancellable((x1, x2) =&gt; (x1 * x2), [2, 5], 30)
setTimeout(cancelFn, cancelTimeMs)

Every 30ms, fn(2, 5) is called. Until t=165ms, then it is cancelled.
1st fn call is at 0ms&nbsp;
Expand All @@ -90,6 +78,8 @@ Cancelled at 165ms
]
<strong>Explanation:</strong>
const cancelTimeMs = 180;
const cancelFn = cancellable((x1, x2, x3) =&gt; (x1 + x2 + x3), [5, 1, 3], 50)
setTimeout(cancelFn, cancelTimeMs)

Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled.
1st fn call is at 0ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ boundFunc(); // &quot;My name is Kathy&quot;
<p><strong>Constraints:</strong></p>

<ul>
<li><code>obj is a non-null object</code></li>
<li><code>obj</code> is a non-null object</li>
<li><code>0 &lt;= inputs.length &lt;= 100</code></li>
</ul>

Expand Down
4 changes: 1 addition & 3 deletions solution/2700-2799/2756.Query Batching/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

<p><img alt="Throttle info" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2756.Query%20Batching/images/throttle.png" style="width: 622px; height: 200px;" /></p>

<p>&nbsp;</p>

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

Expand Down Expand Up @@ -119,7 +117,7 @@ queryMultiple([&#39;f&#39;]) is called at t=350ms, it is resolved at 450ms
<li><code>0 &lt;= t &lt;= 1000</code></li>
<li><code>0 &lt;= calls.length &lt;= 10</code></li>
<li><code>1 &lt;= key.length&nbsp;&lt;= 100</code></li>
<li><code>all keys are unique</code></li>
<li>All keys are unique</li>
</ul>

## Solutions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [2764. 数组是否表示某二叉树的前序遍历](https://leetcode.cn/problems/is-array-a-preorder-of-some-binary-tree)

[English Version](/solution/2700-2799/2764.is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/README_EN.md)
[English Version](/solution/2700-2799/2764.Is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/README_EN.md)

## 题目描述

Expand All @@ -25,7 +25,7 @@
我们可以验证这是树的前序遍历,首先访问节点 0,然后对左子节点进行前序遍历,即 [1] ,然后对右子节点进行前序遍历,即 [2,3,4] 。
</pre>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/1.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.Is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/1.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>

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

Expand All @@ -36,7 +36,7 @@
对于前序遍历,首先访问节点 0,然后对左子节点进行前序遍历,即 [1,3,4],但是我们可以看到在给定的顺序中,2 位于 1 和 3 之间,因此它不是树的前序遍历。
</pre>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/2.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.Is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/2.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# [2764. is Array a Preorder of Some ‌Binary Tree](https://leetcode.com/problems/is-array-a-preorder-of-some-binary-tree)
# [2764. Is Array a Preorder of Some ‌Binary Tree](https://leetcode.com/problems/is-array-a-preorder-of-some-binary-tree)

[中文文档](/solution/2700-2799/2764.is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/README.md)
[中文文档](/solution/2700-2799/2764.Is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/README.md)

## Description

<p>Given a <strong>0-indexed</strong> integer <strong>2D array</strong> <code>nodes</code>, your task is to determine if the given array represents the <strong>preorder</strong> traversal of some <strong>binary</strong> tree.</p>

<p>For each index <code>i</code>, <code>nodes[i] = [id, parentId]</code>, where <code>id</code> is the id of the node at the index <code>i</code> and <code>parentId</code> is the id of its parent in the tree (if the node has no parent, then <code>parentId = -1</code>).</p>
<p>For each index <code>i</code>, <code>nodes[i] = [id, parentId]</code>, where <code>id</code> is the id of the node at the index <code>i</code> and <code>parentId</code> is the id of its parent in the tree (if the node has no parent, then <code>parentId == -1</code>).</p>

<p>Return <code>true</code> <em>if the given array&nbsp;</em><em>represents the preorder traversal of some tree, and</em> <code>false</code> <em>otherwise.</em></p>
<p>Return <code>true</code> <em>if the given array </em><em>represents the preorder traversal of some tree, and</em> <code>false</code> <em>otherwise.</em></p>

<p><strong>Note:</strong> the <strong>preorder</strong> traversal of a tree is a recursive way to traverse a tree in which we first visit the current node, then we do the preorder traversal for the left child, and finally, we do it for the right child.</p>

Expand All @@ -22,7 +22,7 @@
We can show that this is the preorder traversal of the tree, first we visit node 0, then we do the preorder traversal of the right child which is [1], then we do the preorder traversal of the left child which is [2,3,4].
</pre>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/1.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.Is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/1.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>

<p><strong class="example">Example 2:</strong></p>

Expand All @@ -33,7 +33,7 @@ We can show that this is the preorder traversal of the tree, first we visit node
For the preorder traversal, first we visit node 0, then we do the preorder traversal of the right child which is [1,3,4], but we can see that in the given order, 2 comes between 1 and 3, so, it&#39;s not the preorder traversal of the tree.
</pre>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/2.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2764.Is%20Array%20a%20Preorder%20of%20Some%20%E2%80%8CBinary%20Tree/images/2.png" style="padding: 10px; background: #fff; border-radius: .5rem; width: 250px; height: 251px;" /></p>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
Expand Down
3 changes: 3 additions & 0 deletions solution/2700-2799/2774.Array Upper Bound/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<li><code>nums</code>&nbsp;is sorted in ascending order.</li>
</ul>

<p>&nbsp;</p>
<strong>Follow up: </strong>Can you write an algorithm with&nbsp;O(log n)&nbsp;runtime complexity?

## Solutions

<!-- tabs:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

<p>Write a function that accepts another function <code>fn</code> and converts the callback-based function&nbsp;into a promise-based function.&nbsp;</p>

<p>The <code>promisify</code>&nbsp;function takes in a function <code>fn</code> that accepts a callback as its first argument and also any additional arguments. It&nbsp;returns a new function that returns a promise instead. The returned promise should resolve with the result of the original function when the callback is called with a successful response, and reject with the error when the callback is called with an error. The returned promise-based function should accept the additional arguments as inputs.</p>
<p>The function <code>fn</code> takes a callback as its first argument, along with any additional arguments <code>args</code>&nbsp;passed as separate inputs.</p>

<p>The&nbsp;<code>promisify</code>&nbsp;function returns a new function that should return a promise. The promise should resolve with the argument passed as the first parameter of the callback when the callback is invoked without error, and reject with the error when the callback is called with an error as the second argument.</p>

<p>The following is an example of a function that could be passed into&nbsp;<code>promisify</code>.</p>

Expand Down
3 changes: 1 addition & 2 deletions solution/2700-2799/2796.Repeat String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= str.length &lt;= 1000</code></li>
<li><code>1 &lt;= times &lt;= 1000</code></li>
<li><code>1 &lt;= str.length,&nbsp;times &lt;=&nbsp;10<sup>5</sup></code></li>
</ul>

## Solutions
Expand Down
13 changes: 12 additions & 1 deletion solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,15 @@

## 版权

著作权归 [GitHub 开源社区 Doocs](https://github.com/doocs) 所有,商业转载请联系 [@yanglbme](mailto:[email protected]) 获得授权,非商业转载请注明出处。
本项目著作权归 [GitHub 开源社区 Doocs](https://github.com/doocs) 所有,商业转载请联系 @yanglbme 获得授权,非商业转载请注明出处。

## 联系我们

欢迎各位小伙伴们添加 @yanglbme 的个人微信(微信号:YLB0109),备注 「**leetcode**」。后续我们会创建算法、技术相关的交流群,大家一起交流学习,分享经验,共同进步。

| <img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/images/qrcode-for-yanglbme.png" width="260px" align="left"/> |
| ------------------------------------------------------------------------------------------------------------------------------ |

## 许可证

<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>
13 changes: 12 additions & 1 deletion solution/DATABASE_README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,15 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on

## Copyright

[@Doocs](https://github.com/doocs)
The copyright of this project belongs to [Doocs](https://github.com/doocs) community. For commercial reprints, please contact [@yanglbme](mailto:[email protected]) for authorization. For non-commercial reprints, please indicate the source.

## Contact Us

We welcome everyone to add @yanglbme's personal WeChat (WeChat ID: YLB0109), with the note "leetcode". In the future, we will create algorithm and technology related discussion groups, where we can learn and share experiences together, and make progress together.

| <img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/images/qrcode-for-yanglbme-en.png" width="260px" align="left"/> |
| --------------------------------------------------------------------------------------------------------------------------------- |

## License

This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.
13 changes: 12 additions & 1 deletion solution/JAVASCRIPT_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,15 @@

## 版权

著作权归 [GitHub 开源社区 Doocs](https://github.com/doocs) 所有,商业转载请联系 [@yanglbme](mailto:[email protected]) 获得授权,非商业转载请注明出处。
本项目著作权归 [GitHub 开源社区 Doocs](https://github.com/doocs) 所有,商业转载请联系 @yanglbme 获得授权,非商业转载请注明出处。

## 联系我们

欢迎各位小伙伴们添加 @yanglbme 的个人微信(微信号:YLB0109),备注 「**leetcode**」。后续我们会创建算法、技术相关的交流群,大家一起交流学习,分享经验,共同进步。

| <img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/images/qrcode-for-yanglbme.png" width="260px" align="left"/> |
| ------------------------------------------------------------------------------------------------------------------------------ |

## 许可证

<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>
Loading

0 comments on commit 3d74b5a

Please sign in to comment.