1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 10:38:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2023-06-12 23:05:37 +08:00
parent 96cb474414
commit 952a47471f
107 changed files with 22284 additions and 13277 deletions

View File

@ -1,6 +1,6 @@
# 力扣题库(完整版)
> 最后更新日期: **2023.06.02**
> 最后更新日期: **2023.06.12**
>
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,58 @@
{
"data": {
"question": {
"questionId": "2874",
"questionFrontendId": "2720",
"categoryTitle": "Database",
"boundTopicId": 2299616,
"title": "Popularity Percentage",
"titleSlug": "popularity-percentage",
"content": null,
"translatedTitle": "受欢迎度百分比",
"translatedContent": null,
"isPaidOnly": true,
"difficulty": "Hard",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": null,
"topicTags": [],
"companyTagStats": null,
"codeSnippets": null,
"stats": "{\"totalAccepted\": \"26\", \"totalSubmission\": \"31\", \"totalAcceptedRaw\": 26, \"totalSubmissionRaw\": 31, \"acRate\": \"83.9%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "{\"headers\":{\"Friends\":[\"user1\",\"user2\"]},\"rows\":{\"Friends\":[[2,1],[1,3],[4,1],[1,5],[1,6],[2,6],[7,2],[8,3],[3,9]]}}",
"metaData": "{\n \"mysql\": [\n \"Create table if not exists Friends (user1 int, user2 int)\"\n ],\n \"mssql\": [\n \"Create table Friends(user1 int, user2 int)\"\n ],\n \"oraclesql\": [\n \"Create table Friends(user1 int, user2 int)\"\n ],\n \"database\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [
"Create table if not exists Friends (user1 int, user2 int)",
"Truncate table Friends",
"insert into Friends (user1, user2) values ('2', '1')",
"insert into Friends (user1, user2) values ('1', '3')",
"insert into Friends (user1, user2) values ('4', '1')",
"insert into Friends (user1, user2) values ('1', '5')",
"insert into Friends (user1, user2) values ('1', '6')",
"insert into Friends (user1, user2) values ('2', '6')",
"insert into Friends (user1, user2) values ('7', '2')",
"insert into Friends (user1, user2) values ('8', '3')",
"insert into Friends (user1, user2) values ('3', '9')"
],
"enableRunCode": true,
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"Friends\":[\"user1\",\"user2\"]},\"rows\":{\"Friends\":[[2,1],[1,3],[4,1],[1,5],[1,6],[2,6],[7,2],[8,3],[3,9]]}}",
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2859",
"questionFrontendId": "2723",
"categoryTitle": "JavaScript",
"boundTopicId": 2302687,
"title": "Add Two Promises",
"titleSlug": "add-two-promises",
"content": "Given two promises <code>promise1</code> and <code>promise2</code>, return a new promise. <code>promise1</code> and <code>promise2</code>&nbsp;will both resolve with a number. The returned promise should resolve with the sum of the two numbers.\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \npromise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(2), 20)), \npromise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 60))\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \npromise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 50)), \npromise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(-12), 30))\n<strong>Output:</strong> -2\n<strong>Explanation:</strong> The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>promise1 and promise2 are&nbsp;promises that resolve&nbsp;with a number</code></li>\n</ul>\n",
"translatedTitle": "添加两个 Promise 对象",
"translatedContent": "给定两个 promise 对象&nbsp;<code>promise1</code> 和 <code>promise2</code>,返回一个新的 promise。<code>promise1</code> 和 <code>promise2</code> 都会被解析为一个数字。返回的 Promise 应该解析为这两个数字的和。\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>\npromise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(2), 20)), \npromise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 60))\n<b>输出:</b>7\n<b>解释:</b>两个输入的 Promise 分别解析为值 2 和 5。返回的 Promise 应该解析为 2 + 5 = 7。返回的 Promise 解析的时间不作为判断条件。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>\npromise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 50)), \npromise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(-12), 30))\n<b>输出:</b>-2\n<b>解释:</b>两个输入的 Promise 分别解析为值 10 和 -12。返回的 Promise 应该解析为 10 + -12 = -2。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>promise1 和 promise2 都是被解析为一个数字的 promise 对象</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Promise} promise1\n * @param {Promise} promise2\n * @return {Promise}\n */\nvar addTwoPromises = async function(promise1, promise2) {\n \n};\n\n/**\n * addTwoPromises(Promise.resolve(2), Promise.resolve(2))\n * .then(console.log); // 4\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "async function addTwoPromises(promise1: Promise<number>, promise2: Promise<number>): Promise<number> {\n\n};\n\n/**\n * addTwoPromises(Promise.resolve(2), Promise.resolve(2))\n * .then(console.log); // 4\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"161\", \"totalSubmission\": \"177\", \"totalAcceptedRaw\": 161, \"totalSubmissionRaw\": 177, \"acRate\": \"91.0%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "new Promise(resolve => setTimeout(() => resolve(2), 20))\nnew Promise(resolve => setTimeout(() => resolve(5), 60))",
"metaData": "{\n \"name\": \"addTwoPromises\",\n \"params\": [\n {\n \"name\": \"promise1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"promise2\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "new Promise(resolve => setTimeout(() => resolve(2), 20))\nnew Promise(resolve => setTimeout(() => resolve(5), 60))\nnew Promise(resolve => setTimeout(() => resolve(10), 50))\nnew Promise(resolve => setTimeout(() => resolve(-12), 30))",
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2863",
"questionFrontendId": "2726",
"categoryTitle": "JavaScript",
"boundTopicId": 2300583,
"title": "Calculator with Method Chaining",
"titleSlug": "calculator-with-method-chaining",
"content": "<p>Design a <code>Calculator</code> class. The class should provide the&nbsp;mathematical operations of&nbsp;addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining.&nbsp;The <code>Calculator</code> class constructor should accept a number&nbsp;which serves as the&nbsp;initial value of <code>result</code>.</p>\n\n<p>Your <font face=\"monospace\"><code>Calculator</code>&nbsp;</font>class should have the following methods:</p>\n\n<ul>\n\t<li><code>add</code> - This method adds the given number <code>value</code> to the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>subtract</code> -&nbsp;This method subtracts the given number <code>value</code>&nbsp;from the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>multiply</code> -&nbsp;This method multiplies the <code>result</code>&nbsp; by the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>divide</code> -&nbsp;This method divides the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>. If the passed value is <code>0</code>, an error <code>&quot;Division by zero is not allowed&quot;</code> should be thrown.</li>\n\t<li><code>power</code> -&nbsp;This method raises the&nbsp;<code>result</code> to the power of the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>getResult</code> -&nbsp;This method returns the <code>result</code>.</li>\n</ul>\n\n<p>Solutions within&nbsp;<code>10<sup>-5</sup></code>&nbsp;of the actual result are considered correct.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;getResult&quot;], values = [10, 5, 7]\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> \nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;multiply&quot;, &quot;power&quot;, &quot;getResult&quot;], values = [2, 5, 2]\n<strong>Output:</strong> 100\n<strong>Explanation:</strong> \nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;divide&quot;, &quot;getResult&quot;], values = [20, 0]\n<strong>Output:</strong> &quot;Division by zero is not allowed&quot;\n<strong>Explanation:</strong> \nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\nThe error should be thrown because we cannot divide by zero.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup></code><code>&nbsp;- 1</code></li>\n\t<li><code>actions[i] is one of &quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;multiply&quot;, &quot;divide&quot;, &quot;power&quot;, and&nbsp;&quot;getResult&quot;</code></li>\n\t<li><code><font face=\"monospace\">Last action is always &quot;getResult&quot;</font></code></li>\n\t<li><code><font face=\"monospace\">values is a JSON array of numbers</font></code></li>\n</ul>\n",
"translatedTitle": "使用方法链的计算器",
"translatedContent": "<p>设计一个类 <code>Calculator</code> 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。<code>Calculator</code> 类的构造函数应接受一个数字作为 <code>result</code> 的初始值。</p>\n\n<p>你的 <code>Calculator</code> 类应包含以下方法:</p>\n\n<ul>\n\t<li><code>add</code> - 将给定的数字 <code>value</code> 与 <code>result</code> 相加,并返回更新后的 <code>Calculator</code> 对象。</li>\n\t<li><code>subtract</code> - 从 <code>result</code> 中减去给定的数字 <code>value</code>&nbsp;,并返回更新后的 <code>Calculator</code> 对象。</li>\n\t<li><code>multiply</code> - 将 <code>result</code> 乘以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。</li>\n\t<li><code>divide</code> - 将 <code>result</code> 除以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。如果传入的值为 <code>0</code> ,则抛出错误 <code>\"Division by zero is not allowed\"</code> 。</li>\n\t<li><code>power</code> - 将 <code>result</code> 的值乘以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。</li>\n\t<li><code>getResult</code> - 返回 <code>result</code> 的值。</li>\n</ul>\n\n<p>结果与实际结果相差在 <code>10<sup>-5</sup></code><sup>&nbsp;</sup>范围内的解被认为是正确的。</p>\n\n<p>&nbsp;</p>\n\n<p><b>示例 1</b></p>\n\n<pre>\n<b>输入:</b>actions = [\"Calculator\", \"add\", \"subtract\", \"getResult\"], values = [10, 5, 7]\n<b>输出:</b>8\n<b>解释:</b>\nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>actions = [\"Calculator\", \"multiply\", \"power\", \"getResult\"], values = [2, 5, 2]\n<b>输出:</b>100\n<b>解释:</b>\nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>actions = [\"Calculator\", \"divide\", \"getResult\"], values = [20, 0]\n<b>输出:</b>\"Division by zero is not allowed\"\n<b>解释:</b>\nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\n由于不能除以零因此应抛出错误。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup></code><code>&nbsp;- 1</code></li>\n\t<li><code>actions[i] 是 \"Calculator\", \"add\", \"subtract\", \"multiply\", \"divide\", \"power\", 和 \"getResult\" 其中的元素</code></li>\n\t<li><code><font face=\"monospace\">最后一个操作总是&nbsp;\"getResult\"</font></code></li>\n\t<li><code><font face=\"monospace\">values </font>是一个数字的 JSON 数组</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "class Calculator {\n \n /** \n * @param {number} value\n */\n constructor(value) {\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n add(value){\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n subtract(value){\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */ \n multiply(value) {\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n divide(value) {\n\n }\n \n /** \n * @param {number} value\n * @return {Calculator}\n */\n power(value) {\n\n }\n \n /** \n * @return {number}\n */\n getResult() {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "class Calculator {\n \n constructor(value : number) {\n\n }\n \n add(value : number) : Calculator {\n\n }\n \n subtract(value : number) : Calculator {\n\n }\n \n multiply(value : number) : Calculator {\n\n }\n\n divide(value : number) : Calculator {\n\n }\n \n power(value : number) : Calculator {\n\n }\n\n getResult() : number {\n \n }\n}",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"124\", \"totalSubmission\": \"192\", \"totalAcceptedRaw\": 124, \"totalSubmissionRaw\": 192, \"acRate\": \"64.6%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]",
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"actions\",\n \"type\": \"string[]\"\n },\n {\n \"type\": \"double[]\",\n \"name\": \"values\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]\n[\"Calculator\", \"multiply\", \"power\", \"getResult\"]\n[2, 5, 2]\n[\"Calculator\", \"divide\", \"getResult\"]\n[20, 0]",
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2807",
"questionFrontendId": "2721",
"categoryTitle": "JavaScript",
"boundTopicId": 2302685,
"title": "Execute Asynchronous Functions in Parallel",
"titleSlug": "execute-asynchronous-functions-in-parallel",
"content": "<p>Given an array of asynchronous&nbsp;functions&nbsp;<code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments&nbsp;and returns a promise.</p>\n\n<p><code>promise</code> resolves:</p>\n\n<ul>\n\t<li>When all the promises returned from&nbsp;<code>functions</code>&nbsp;were resolved successfully.&nbsp;The resolved&nbsp;value of&nbsp;<code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the&nbsp;<code>functions.</code></li>\n</ul>\n\n<p><code>promise</code> rejects:</p>\n\n<ul>\n\t<li>When any&nbsp;of the promises&nbsp;returned from&nbsp;<code>functions</code>&nbsp;were rejected.&nbsp;<code>promise</code> should also&nbsp;reject&nbsp;with the reason of the first rejection.</li>\n</ul>\n\n<p>Please solve it without using the built-in&nbsp;<code>Promise.all</code>&nbsp;function.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))\n]\n<strong>Output:</strong> {&quot;t&quot;: 200, &quot;resolved&quot;: [5]}\n<strong>Explanation:</strong> \npromiseAll(functions).then(console.log); // [5]\n\nThe single function was resolved at 200ms with a value of 5.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)), \n () =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(&quot;Error&quot;), 100))\n]\n<strong>Output:</strong> {&quot;t&quot;: 100, &quot;rejected&quot;: &quot;Error&quot;}\n<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)), \n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)), \n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))\n]\n<strong>Output:</strong> {&quot;t&quot;: 150, &quot;resolved&quot;: [4, 10, 16]}\n<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>functions&nbsp;is an array of functions that returns promises</code></li>\n\t<li><code>1 &lt;= functions.length &lt;= 10</code></li>\n</ul>\n",
"translatedTitle": "并行执行异步函数",
"translatedContent": "<p>给定一个异步函数数组 <code>functions</code>,返回一个新的 promise 对象&nbsp;<code>promise</code>。数组中的每个函数都不接受参数并返回一个 promise。</p>\n\n<p><code>promise</code> resolve 条件:</p>\n\n<ul>\n\t<li>当所有从 <code>functions</code> 返回的 promise 都成功解析时。<code>promise</code> 的解析值应该是一个按照它们在 <code>functions</code> 中的顺序排列的 promise 的解析值数组。</li>\n</ul>\n\n<p><code>promise</code>&nbsp;reject 条件:</p>\n\n<ul>\n\t<li>当任何从 <code>functions</code> 返回的 promise 被拒绝时。<code>promise</code> 也会被拒绝,并返回第一个拒绝的原因。</li>\n</ul>\n\n<p>请在不使用内置的 <code>Promise.all</code> 函数的情况下解决。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>functions = [\n&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))\n]\n<b>输出:</b>{\"t\": 200, \"resolved\": [5]}\n<b>解释:</b>\npromiseAll(functions).then(console.log); // [5]\n\n单个函数在 200 毫秒后以值 5 成功解析。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>functions = [\n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)), \n () =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(\"Error\"), 100))\n]\n<b>输出:</b>{\"t\": 100, \"rejected\": \"Error\"}\n<b>解释:</b>由于其中一个 promise 被拒绝,返回的 promise 也在同一时间被拒绝并返回相同的错误。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>functions = [\n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)), \n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)), \n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))\n]\n<b>输出:</b>{\"t\": 150, \"resolved\": [4, 10, 16]}\n<b>解释:</b>所有的 promise 都成功执行。当最后一个 promise 被解析时,返回的 promise 也被解析了。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>functions&nbsp;is an array of functions that returns promises</code></li>\n\t<li><code>1 &lt;= functions.length &lt;= 10</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = async function(functions) {\n \n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "async function promiseAll<T>(functions: (() => Promise<T>)[]): Promise<T[]> {\n\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"75\", \"totalSubmission\": \"102\", \"totalAcceptedRaw\": 75, \"totalSubmissionRaw\": 102, \"acRate\": \"73.5%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[() => new Promise(resolve => setTimeout(() => resolve(5), 200))]",
"metaData": "{\n \"name\": \"promiseAll\",\n \"params\": [\n {\n \"name\": \"functions\",\n \"type\": \"string[]\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[() => new Promise(resolve => setTimeout(() => resolve(5), 200))]\n[() => new Promise(resolve => setTimeout(() => resolve(1), 200)), () => new Promise((resolve, reject) => setTimeout(() => reject(\"Error\"), 100))]\n[() => new Promise(resolve => setTimeout(() => resolve(4), 50)), () => new Promise(resolve => setTimeout(() => resolve(10), 150)), () => new Promise(resolve => setTimeout(() => resolve(16), 100))]",
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2821",
"questionFrontendId": "2715",
"categoryTitle": "JavaScript",
"boundTopicId": 2293968,
"title": "Execute Cancellable Function With Delay",
"titleSlug": "execute-cancellable-function-with-delay",
"content": "<p>Given a function <code>fn</code>, an array or arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>\n\n<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case,&nbsp;<code>fn</code> should never be called.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50\n<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]\n<strong>Explanation:</strong> \nconst cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms\nsetTimeout(cancel, 50);\n\nthe cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50\n<strong>Output:</strong> []\n<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100\n<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]\n<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime &gt; t.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn is a function</code></li>\n\t<li><code>args is a valid JSON array</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"translatedTitle": "执行可取消的延迟函数",
"translatedContent": "<p>现给定一个函数 <code>fn</code>&nbsp;,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code> 。</p>\n\n<p>在经过 <code>t</code> 毫秒的延迟后,<strong>除非</strong> 先调用 <code>cancelFn</code> ,否则&nbsp;<code>fn</code> 应该以 <code>args</code> 作为参数被调用。并且在这种情况下,<code>fn</code> 不应该被调用。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50\n<b>输出:</b>[{\"time\": 20, \"returned\": 10}]\n<b>解释:</b>\nconst cancel = cancellable(fn, [2], 20); // // 在 t=20ms 时调用 fn(2)\nsetTimeout(cancel, 50);\n\ncancelTime50ms在延迟时间20ms之后所以 fn(2) 应该在 t=20ms 时调用。fn 的返回值是 10。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50\n<b>输出:</b>[]\n<b>解释:</b>fn(2) 从未被调用,因为 cancelTime50ms在延迟时间100ms之前。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100\n<b>输出:</b>[{\"time\": 30, \"returned\": 8}]\n<b>解释:</b>fn(2) 从未被调用,因为 cancelTime50ms在延迟时间100ms之前。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>fn 是一个函数</code></li>\n\t<li><code>args 是一个有效的 JSON 数组</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"183\", \"totalSubmission\": \"216\", \"totalAcceptedRaw\": 183, \"totalSubmissionRaw\": 216, \"acRate\": \"84.7%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "(x) => x * 5\n[2]\n20\n50",
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "(x) => x * 5\n[2]\n20\n50\n(x) => x**2\n[2]\n100\n50\n(x1, x2) => x1 * x2\n[2,4]\n20\n100",
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2862",
"questionFrontendId": "2725",
"categoryTitle": "JavaScript",
"boundTopicId": 2300584,
"title": "Interval Cancellation",
"titleSlug": "interval-cancellation",
"content": "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>. 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 <code>cancelFn</code> is called.\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 20, cancelT = 110\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 8},\n {&quot;time&quot;: 20, &quot;returned&quot;: 8},\n {&quot;time&quot;: 40, &quot;returned&quot;: 8},\n {&quot;time&quot;: 60, &quot;returned&quot;: 8},\n {&quot;time&quot;: 80, &quot;returned&quot;: 8},\n {&quot;time&quot;: 100, &quot;returned&quot;: 8}\n]\n<strong>Explanation:</strong> Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.\n\nconst cancel = cancellable(x =&gt; x * 2, [4], 20);\nsetTimeout(cancel, 110);\n\n1st fn call is at 0ms. fn(4) returns 8.\n2nd fn call is at 20ms. fn(4) returns 8.\n3rd fn call is at 40ms. fn(4) returns 8.\n4th fn call is at&nbsp;60ms. fn(4) returns 8.\n5th fn call is at 80ms. fn(4) returns 8.\n6th fn call is at 100ms. fn(4) returns 8.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 25, cancelT = 140\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 10},\n {&quot;time&quot;: 25, &quot;returned&quot;: 10},\n {&quot;time&quot;: 50, &quot;returned&quot;: 10},\n {&quot;time&quot;: 75, &quot;returned&quot;: 10},\n {&quot;time&quot;: 100, &quot;returned&quot;: 10},\n {&quot;time&quot;: 125, &quot;returned&quot;: 10}\n]\n<strong>Explanation:</strong> Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.\n1st fn call is at 0ms&nbsp;\n2nd fn call is at 25ms&nbsp;\n3rd fn call is at 50ms&nbsp;\n4th fn call is at&nbsp;75ms&nbsp;\n5th fn call is at 100ms&nbsp;\n6th fn call is at 125ms\nCancelled at 140ms\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 9},\n {&quot;time&quot;: 50, &quot;returned&quot;: 9},\n {&quot;time&quot;: 100, &quot;returned&quot;: 9},\n {&quot;time&quot;: 150, &quot;returned&quot;: 9}\n]\n<strong>Explanation:</strong> Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled. \n1st fn call is at 0ms\n2nd fn call is at 50ms\n3rd fn call is at 100ms\n4th fn call is at&nbsp;150ms\nCancelled at 180ms\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn is a function</code></li>\n\t<li><code>args is a valid JSON array</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"translatedTitle": "间隔取消",
"translatedContent": "现给定一个函数 <code>fn</code>,一个参数数组 <code>args</code> 和一个时间间隔 <code>t</code>,返回一个取消函数 <code>cancelFn</code>。函数 <code>fn</code> 应该立即使用 <code>args</code> 调用,并且在每个 <code>t</code> 毫秒内再次调用,直到调用 <code>cancelFn</code>。\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) =&gt; x * 2, args = [4], t = 20, cancelT = 110\n<b>输出:</b>\n[\n {\"time\": 0, \"returned\": 8},\n {\"time\": 20, \"returned\": 8},\n {\"time\": 40, \"returned\": 8},\n {\"time\": 60, \"returned\": 8},\n {\"time\": 80, \"returned\": 8},\n {\"time\": 100, \"returned\": 8}\n]\n<strong>解释:</strong> 每隔 20ms调用 fn(4)。直到 t=110ms然后取消。\nconst cancel = cancellable(x =&gt; x * 2, [4], 20);\nsetTimeout(cancel, 110);\n第一次调用 fn 是在 0ms。fn(4) 返回 8。\n第二次调用 fn 是在 20ms。fn(4) 返回 8。\n第三次调用 fn 是在 40ms。fn(4) 返回 8。\n第四次调用 fn 是在&nbsp;60ms。fn(4) 返回 8。\n第五次调用 fn 是在 80ms。fn(4) 返回 8。\n第六次调用 fn 是在 100ms。fn(4) 返回 8。</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 25, cancelT = 140\n<strong>输出:</strong> \n[\n {\"time\": 0, \"returned\": 10},\n {\"time\": 25, \"returned\": 10},\n {\"time\": 50, \"returned\": 10},\n {\"time\": 75, \"returned\": 10},\n {\"time\": 100, \"returned\": 10},\n {\"time\": 125, \"returned\": 10}\n]\n<strong>解释:</strong>每隔 25ms调用 fn(2, 5)。直到 t=140ms然后取消。\n第一次调用 fn 是在 0ms\n第二次调用 fn 是在 25ms\n第三次调用 fn 是在 50ms\n第四次调用 fn 是在&nbsp;75ms\n第五次调用 fn 是在 100ms\n第六次调用 fn 是在 125ms\n在 140ms 取消\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180\n<b>输出:</b>\n[\n {\"time\": 0, \"returned\": 9},\n {\"time\": 50, \"returned\": 9},\n {\"time\": 100, \"returned\": 9},\n {\"time\": 150, \"returned\": 9}\n]\n<b>解释:</b>每隔 50ms调用 fn(5, 1, 3)。直到 t=180ms然后取消。\n第一次调用 fn 是在 0ms\n第二次调用 fn 是在 50ms\n第三次调用 fn 是在 100ms\n第四次调用 fn 是在&nbsp;150ms\n在 180ms 取消\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>fn 是一个函数</code></li>\n\t<li><code>args 是一个有效的 JSON 数组</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 2\n * const args = [4], t = 20, cancelT = 110\n *\n * const start = performance.now()\n *\n * const log = (...argsArr) => {\n *\t\tconst val = fn(...argsArr)\n * result.push({\"time\": Math.floor(performance.now() - start), \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":20,\"returned\":8},\n * // {\"time\":40,\"returned\":8}, \n * // {\"time\":60,\"returned\":8},\n * // {\"time\":80,\"returned\":8},\n * // {\"time\":100,\"returned\":8}\n * // ]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 2\n * const args = [4], t = 20, cancelT = 110\n *\n * const start = performance.now()\n *\n * const log = (...argsArr) => {\n *\t\tconst val = fn(...argsArr)\n * result.push({\"time\": Math.floor(performance.now() - start), \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":20,\"returned\":8},\n * // {\"time\":40,\"returned\":8}, \n * // {\"time\":60,\"returned\":8},\n * // {\"time\":80,\"returned\":8},\n * // {\"time\":100,\"returned\":8}\n * // ]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"139\", \"totalSubmission\": \"176\", \"totalAcceptedRaw\": 139, \"totalSubmissionRaw\": 176, \"acRate\": \"79.0%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "(x) => x * 2\n[4]\n20\n110",
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "(x) => x * 2\n[4]\n20\n110\n(x1, x2) => (x1 * x2)\n[2,5]\n25\n140\n(x1, x2, x3) => (x1 + x2 + x3)\n[5,1,3]\n50\n160",
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2864",
"questionFrontendId": "2727",
"categoryTitle": "JavaScript",
"boundTopicId": 2302689,
"title": "Is Object Empty",
"titleSlug": "is-object-empty",
"content": "<p>Given an object or an array, return if it is empty.</p>\n\n<ul>\n\t<li>An empty object contains no key-value pairs.</li>\n\t<li>An empty array contains no elements.</li>\n</ul>\n\n<p>You may assume the object or array is the output of&nbsp;<code>JSON.parse</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = {&quot;x&quot;: 5, &quot;y&quot;: 42}\n<strong>Output:</strong> false\n<strong>Explanation:</strong> The object has 2 key-value pairs so it is not empty.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = {}\n<strong>Output:</strong> true\n<strong>Explanation:</strong> The object doesn&#39;t have any key-value pairs so it is empty.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = [null, false, 0]\n<strong>Output:</strong> false\n<strong>Explanation:</strong> The array has 3 elements so it is not empty.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>&nbsp;<code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>\n</ul>\n\n<p>&nbsp;</p>\n<strong>Can you solve it in O(1) time?</strong>",
"translatedTitle": "判断对象是否为空",
"translatedContent": "<p>给定一个对象或数组,判断它是否为空。</p>\n\n<ul>\n\t<li>一个空对象不包含任何键值对。</li>\n\t<li>一个空数组不包含任何元素。</li>\n</ul>\n\n<p>你可以假设对象或数组是通过 <code>JSON.parse</code> 解析得到的。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>obj = {\"x\": 5, \"y\": 42}\n<b>输出:</b>false\n<b>解释:</b>The object has 2 key-value pairs so it is not empty.\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>obj = {}\n<b>输出:</b>true\n<b>解释:</b>The object doesn't have any key-value pairs so it is empty.\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>obj = [null, false, 0]\n<b>输出:</b>false\n<b>解释:</b>The array has 3 elements so it is not empty.\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li>&nbsp;<code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>\n</ul>\n\n<p>&nbsp;</p>\n<strong>你可以在 O(1) 时间复杂度内解决这个问题吗?</strong>",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Object | Array} obj\n * @return {boolean}\n */\nvar isEmpty = function(obj) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function isEmpty(obj: Record<string, any> | any[]): boolean {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"165\", \"totalSubmission\": \"206\", \"totalAcceptedRaw\": 165, \"totalSubmissionRaw\": 206, \"acRate\": \"80.1%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "{\"x\": 5, \"y\": 42}",
"metaData": "{\n \"name\": \"isEmpty\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"x\": 5, \"y\": 42}\n{}\n[null, false, 0]",
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2858",
"questionFrontendId": "2722",
"categoryTitle": "JavaScript",
"boundTopicId": 2300585,
"title": "Join Two Arrays by ID",
"titleSlug": "join-two-arrays-by-id",
"content": "<p>Given two arrays <code>arr1</code> and <code>arr2</code>, return a new&nbsp;array <code>joinedArray</code>. All the objects in each&nbsp;of the two inputs arrays will contain an&nbsp;<code>id</code>&nbsp;field that has an integer value.&nbsp;<code>joinedArray</code>&nbsp;is an array formed by merging&nbsp;<code>arr1</code> and <code>arr2</code> based on&nbsp;their <code>id</code>&nbsp;key. The length of&nbsp;<code>joinedArray</code> should be the length of unique values of <code>id</code>. The returned array should be sorted in&nbsp;<strong>ascending</strong>&nbsp;order based on the <code>id</code>&nbsp;key.</p>\n\n<p>If a given&nbsp;<code>id</code>&nbsp;exists in one array but not the other, the single object with that&nbsp;<code>id</code> should be included in the result array without modification.</p>\n\n<p>If two objects share an <code>id</code>, their properties should be merged into a single&nbsp;object:</p>\n\n<ul>\n\t<li>If a key only exists in one object, that single key-value pair should be included in the object.</li>\n\t<li>If a key is included in both objects, the value in the object from <code>arr2</code>&nbsp;should override the value from <code>arr1</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},\n&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9}\n], \narr2 = [\n {&quot;id&quot;: 3, &quot;x&quot;: 5}\n]\n<strong>Output:</strong> \n[\n&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},\n&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9},\n {&quot;id&quot;: 3, &quot;x&quot;: 5}\n]\n<strong>Explanation:</strong> There are no duplicate ids so arr1 is simply concatenated with arr2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n {&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},\n {&quot;id&quot;: 2, &quot;x&quot;: 3, &quot;y&quot;: 6}\n], \narr2 = [\n {&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},\n {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}\n]\n<strong>Output:</strong> \n[\n {&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},\n {&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},\n&nbsp; {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}\n]\n<strong>Explanation:</strong> The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;b&quot;: 94},&quot;v&quot;: [4, 3], &quot;y&quot;: 48}\n]\narr2 = [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3]}\n]\n<strong>Output:</strong> [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3], &quot;y&quot;: 48}\n]\n<strong>Explanation:</strong> The two objects with id=1 are merged together. For the keys &quot;b&quot; and &quot;v&quot; the values from arr2 are used. Since the key &quot;y&quot; only exists in arr1, that value is taken form arr1.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr1 and arr2 are valid JSON arrays</code></li>\n\t<li><code>Each object in arr1 and arr2 has a unique&nbsp;integer id key</code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>\n</ul>\n",
"translatedTitle": "根据 ID 合并两个数组",
"translatedContent": "<p>现给定两个数组 <code>arr1</code> 和 <code>arr2</code> ,返回一个新的数组 <code>joinedArray</code> 。两个输入数组中的每个对象都包含一个 <code>id</code> 字段。<code>joinedArray</code> 是一个通过 <code>id</code> 将 <code>arr1</code> 和 <code>arr2</code> 连接而成的数组。<code>joinedArray</code> 的长度应为唯一值 <code>id</code> 的长度。返回的数组应按 <code>id</code> <strong>升序</strong> 排序。</p>\n\n<p>如果一个 <code>id</code> 存在于一个数组中但不存在于另一个数组中,则该对象应包含在结果数组中且不进行修改。</p>\n\n<p>如果两个对象共享一个 <code>id</code> ,则它们的属性应进行合并:</p>\n\n<ul>\n\t<li>如果一个键只存在于一个对象中,则该键值对应该包含在对象中。</li>\n\t<li>如果一个键在两个对象中都包含,则 <code>arr2</code> 中的值应覆盖 <code>arr1</code> 中的值。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>\narr1 = [\n&nbsp; {\"id\": 1, \"x\": 1},\n&nbsp; {\"id\": 2, \"x\": 9}\n], \narr2 = [\n {\"id\": 3, \"x\": 5}\n]\n<b>输出:</b>\n[\n&nbsp; {\"id\": 1, \"x\": 1},\n&nbsp; {\"id\": 2, \"x\": 9},\n {\"id\": 3, \"x\": 5}\n]\n<b>解释:</b>没有共同的 id因此将 arr1 与 arr2 简单地连接起来。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>\narr1 = [\n {\"id\": 1, \"x\": 2, \"y\": 3},\n {\"id\": 2, \"x\": 3, \"y\": 6}\n], \narr2 = [\n {\"id\": 2, \"x\": 10, \"y\": 20},\n {\"id\": 3, \"x\": 0, \"y\": 0}\n]\n<b>输出:</b>\n[\n {\"id\": 1, \"x\": 2, \"y\": 3},\n {\"id\": 2, \"x\": 10, \"y\": 20},\n&nbsp; {\"id\": 3, \"x\": 0, \"y\": 0}\n]\n<b>解释:</b>id 为 1 和 id 为 3 的对象在结果数组中保持不变。id 为 2 的两个对象合并在一起。arr2 中的键覆盖 arr1 中的值。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>\narr1 = [\n {\"id\": 1, \"b\": {\"b\": 94},\"v\": [4, 3], \"y\": 48}\n]\narr2 = [\n {\"id\": 1, \"b\": {\"c\": 84}, \"v\": [1, 3]}\n]\n<strong>输出:</strong> [\n {\"id\": 1, \"b\": {\"c\": 84}, \"v\": [1, 3], \"y\": 48}\n]\n<b>解释:</b>具有 id 为 1 的对象合并在一起。对于键 \"b\" 和 \"v\" ,使用 arr2 中的值。由于键 \"y\" 只存在于 arr1 中,因此取 arr1 的值。</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>arr1 和 arr2 都是有效的 JSON 数组</code></li>\n\t<li><code>在 arr1 和 arr2 中都有唯一的键值 id</code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array} arr1\n * @param {Array} arr2\n * @return {Array}\n */\nvar join = function(arr1, arr2) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function join(arr1: any[], arr2: any[]): any[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"115\", \"totalSubmission\": \"214\", \"totalAcceptedRaw\": 115, \"totalSubmissionRaw\": 214, \"acRate\": \"53.7%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]",
"metaData": "{\n \"name\": \"join\",\n \"params\": [\n {\n \"name\": \"arr1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"arr2\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]\n[{\"id\": 1,\"x\": 2,\"y\": 3},{\"id\": 2,\"x\": 3,\"y\": 6}]\n[{\"id\": 2,\"x\": 10,\"y\": 20},{\"id\": 3,\"x\": 0,\"y\": 0}]\n[{\"id\":1,\"b\":{\"b\": 94},\"v\":[4,3],\"y\":48}]\n[{\"id\":1,\"b\":{\"c\": 84},\"v\":[1,3]}]",
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2860",
"questionFrontendId": "2724",
"categoryTitle": "JavaScript",
"boundTopicId": 2302688,
"title": "Sort By",
"titleSlug": "sort-by",
"content": "<p>Given an array <code>arr</code> and a function <code>fn</code>, return a sorted array <code>sortedArr</code>. You can assume&nbsp;<code>fn</code>&nbsp;only returns numbers and those numbers determine the sort order of&nbsp;<code>sortedArr</code>. <code>sortedArray</code> must be sorted in <strong>ascending order</strong> by <code>fn</code> output.</p>\n\n<p>You may assume that <code>fn</code> will never duplicate numbers for a given array.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [5, 4, 1, 2, 3], fn = (x) =&gt; x\n<strong>Output:</strong> [1, 2, 3, 4, 5]\n<strong>Explanation:</strong> fn simply returns the number passed to it so the array is sorted in ascending order.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [{&quot;x&quot;: 1}, {&quot;x&quot;: 0}, {&quot;x&quot;: -1}], fn = (d) =&gt; d.x\n<strong>Output:</strong> [{&quot;x&quot;: -1}, {&quot;x&quot;: 0}, {&quot;x&quot;: 1}]\n<strong>Explanation:</strong> fn returns the value for the &quot;x&quot; key. So the array is sorted based on that value.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [[3, 4], [5, 2], [10, 1]], fn = (x) =&gt; x[1]\n<strong>Output:</strong> [[10, 1], [5, 2], [3, 4]]\n<strong>Explanation:</strong> arr is sorted in ascending order by number at index=1.&nbsp;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr is a valid JSON array</code></li>\n\t<li><code>fn is a function that returns a number</code></li>\n\t<li><code>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>\n</ul>\n",
"translatedTitle": "排序方式",
"translatedContent": "<p>给定一个数组 <code>arr</code> 和一个函数 <code>fn</code>,返回一个排序后的数组 <code>sortedArr</code>。你可以假设 <code>fn</code> 只返回数字,并且这些数字决定了 <code>sortedArr</code> 的排序顺序。<code>sortedArr</code> 必须按照 <code>fn</code> 的输出值&nbsp;<strong>升序</strong> 排序。</p>\n\n<p>你可以假设对于给定的数组,<code>fn</code> 不会返回重复的数字。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>arr = [5, 4, 1, 2, 3], fn = (x) =&gt; x\n<b>输出:</b>[1, 2, 3, 4, 5]\n<b>解释:</b>fn 只是返回传入的数字,因此数组按升序排序。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>arr = [{\"x\": 1}, {\"x\": 0}, {\"x\": -1}], fn = (d) =&gt; d.x\n<b>输出:</b>[{\"x\": -1}, {\"x\": 0}, {\"x\": 1}]\n<b>解释:</b>fn 返回 \"x\" 键的值,因此数组根据该值排序。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>arr = [[3, 4], [5, 2], [10, 1]], fn = (x) =&gt; x[1]\n<b>输出:</b>[[10, 1], [5, 2], [3, 4]]\n<b>解释:</b>数组按照索引为 1 处的数字升序排序。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>arr 是一个有效的 JSON 数组</code></li>\n\t<li><code>fn 是一个函数,返回一个数字</code></li>\n\t<li><code>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */\nvar sortBy = function(arr, fn) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function sortBy(arr: any[], fn: Function): any[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"114\", \"totalSubmission\": \"133\", \"totalAcceptedRaw\": 114, \"totalSubmissionRaw\": 133, \"acRate\": \"85.7%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[5,4,1,2,3]\n(x) => x",
"metaData": "{\n \"name\": \"sortBy\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[5,4,1,2,3]\n(x) => x\n[{\"x\":1},{\"x\": 0},{\"x\": -1}]\n(x) => x.x\n[[3,4],[5,2],[10,1]]\n(x) => x[1]",
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,57 @@
<p>设计一个类 <code>Calculator</code> 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。<code>Calculator</code> 类的构造函数应接受一个数字作为 <code>result</code> 的初始值。</p>
<p>你的 <code>Calculator</code> 类应包含以下方法:</p>
<ul>
<li><code>add</code> - 将给定的数字 <code>value</code><code>result</code> 相加,并返回更新后的 <code>Calculator</code> 对象。</li>
<li><code>subtract</code> - 从 <code>result</code> 中减去给定的数字 <code>value</code>&nbsp;,并返回更新后的 <code>Calculator</code> 对象。</li>
<li><code>multiply</code> - 将 <code>result</code> 乘以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。</li>
<li><code>divide</code> - 将 <code>result</code> 除以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。如果传入的值为 <code>0</code> ,则抛出错误 <code>"Division by zero is not allowed"</code></li>
<li><code>power</code> - 将 <code>result</code> 的值乘以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。</li>
<li><code>getResult</code> - 返回 <code>result</code> 的值。</li>
</ul>
<p>结果与实际结果相差在 <code>10<sup>-5</sup></code><sup>&nbsp;</sup>范围内的解被认为是正确的。</p>
<p>&nbsp;</p>
<p><b>示例 1</b></p>
<pre>
<b>输入:</b>actions = ["Calculator", "add", "subtract", "getResult"], values = [10, 5, 7]
<b>输出:</b>8
<b>解释:</b>
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>actions = ["Calculator", "multiply", "power", "getResult"], values = [2, 5, 2]
<b>输出:</b>100
<b>解释:</b>
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>actions = ["Calculator", "divide", "getResult"], values = [20, 0]
<b>输出:</b>"Division by zero is not allowed"
<b>解释:</b>
new Calculator(20).divide(0).getResult() // 20 / 0
由于不能除以零,因此应抛出错误。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup></code><code>&nbsp;- 1</code></li>
<li><code>actions[i] 是 "Calculator", "add", "subtract", "multiply", "divide", "power", 和 "getResult" 其中的元素</code></li>
<li><code><font face="monospace">最后一个操作总是&nbsp;"getResult"</font></code></li>
<li><code><font face="monospace">values </font>是一个数字的 JSON 数组</code></li>
</ul>

View File

@ -0,0 +1,35 @@
<p>给你一个三位数整数 <code>n</code>&nbsp;</p>
<p>如果经过以下修改得到的数字 <strong>恰好</strong>&nbsp;包含数字 <code>1</code>&nbsp;<code>9</code>&nbsp;各一次且不包含任何 <code>0</code>&nbsp;,那么我们称数字 <code>n</code>&nbsp;<strong>迷人的</strong>&nbsp;</p>
<ul>
<li>&nbsp;<code>n</code>&nbsp;与数字&nbsp;<code>2 * n</code>&nbsp;<code>3 * n</code>&nbsp;<strong>连接</strong>&nbsp;</li>
</ul>
<p>如果 <code>n</code>&nbsp;是迷人的,返回&nbsp;<code>true</code>,否则返回&nbsp;<code>false</code>&nbsp;</p>
<p><strong>连接</strong>&nbsp;两个数字表示把它们首尾相接连在一起。比方说&nbsp;<code>121</code>&nbsp;<code>371</code>&nbsp;连接得到&nbsp;<code>121371</code>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>n = 192
<b>输出:</b>true
<b>解释:</b>我们将数字 n = 192 2 * n = 384 和 3 * n = 576 连接,得到 192384576 。这个数字包含 1 到 9 恰好各一次。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>n = 100
<b>输出:</b>false
<b>解释:</b>我们将数字 n = 100 2 * n = 200 和 3 * n = 300 连接,得到 100200300 。这个数字不符合上述条件。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>100 &lt;= n &lt;= 999</code></li>
</ul>

View File

@ -0,0 +1,45 @@
<p>给定一个对象或数组,判断它是否为空。</p>
<ul>
<li>一个空对象不包含任何键值对。</li>
<li>一个空数组不包含任何元素。</li>
</ul>
<p>你可以假设对象或数组是通过 <code>JSON.parse</code> 解析得到的。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>obj = {"x": 5, "y": 42}
<b>输出:</b>false
<b>解释:</b>The object has 2 key-value pairs so it is not empty.
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>obj = {}
<b>输出:</b>true
<b>解释:</b>The object doesn't have any key-value pairs so it is empty.
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>obj = [null, false, 0]
<b>输出:</b>false
<b>解释:</b>The array has 3 elements so it is not empty.
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>&nbsp;<code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>
</ul>
<p>&nbsp;</p>
<strong>你可以在 O(1) 时间复杂度内解决这个问题吗?</strong>

View File

@ -0,0 +1,54 @@
<p>给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的整数排列 <code>nums</code></p>
<p>如果排列的第一个数字等于 <code>1</code> 且最后一个数字等于 <code>n</code> ,则称其为 <strong>半有序排列</strong> 。你可以执行多次下述操作,直到将 <code>nums</code> 变成一个 <strong>半有序排列</strong> </p>
<ul>
<li>选择 <code>nums</code> 中相邻的两个元素,然后交换它们。</li>
</ul>
<p>返回使 <code>nums</code> 变成 <strong>半有序排列</strong> 所需的最小操作次数。</p>
<p><strong>排列</strong> 是一个长度为 <code>n</code> 的整数序列,其中包含从 <code>1</code><code>n</code> 的每个数字恰好一次。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [2,1,4,3]
<strong>输出:</strong>2
<strong>解释:</strong>可以依次执行下述操作得到半有序排列:
1 - 交换下标 0 和下标 1 对应元素。排列变为 [1,2,4,3] 。
2 - 交换下标 2 和下标 3 对应元素。排列变为 [1,2,3,4] 。
可以证明,要让 nums 成为半有序排列,不存在执行操作少于 2 次的方案。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [2,4,1,3]
<strong>输出:</strong>3
<strong>解释:
</strong>可以依次执行下述操作得到半有序排列:
1 - 交换下标 1 和下标 2 对应元素。排列变为 [2,1,4,3] 。
2 - 交换下标 0 和下标 1 对应元素。排列变为 [1,2,4,3] 。
3 - 交换下标 2 和下标 3 对应元素。排列变为 [1,2,3,4] 。
可以证明,要让 nums 成为半有序排列,不存在执行操作少于 3 次的方案。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [1,3,4,2,5]
<strong>输出:</strong>0
<strong>解释:</strong>这个排列已经是一个半有序排列,无需执行任何操作。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= nums.length == n &lt;= 50</code></li>
<li><code>1 &lt;= nums[i]&nbsp;&lt;= 50</code></li>
<li><code>nums</code> 是一个 <strong>排列</strong></li>
</ul>

View File

@ -0,0 +1,62 @@
<p>给定一个异步函数数组 <code>functions</code>,返回一个新的 promise 对象&nbsp;<code>promise</code>。数组中的每个函数都不接受参数并返回一个 promise。</p>
<p><code>promise</code> resolve 条件:</p>
<ul>
<li>当所有从 <code>functions</code> 返回的 promise 都成功解析时。<code>promise</code> 的解析值应该是一个按照它们在 <code>functions</code> 中的顺序排列的 promise 的解析值数组。</li>
</ul>
<p><code>promise</code>&nbsp;reject 条件:</p>
<ul>
<li>当任何从 <code>functions</code> 返回的 promise 被拒绝时。<code>promise</code> 也会被拒绝,并返回第一个拒绝的原因。</li>
</ul>
<p>请在不使用内置的 <code>Promise.all</code> 函数的情况下解决。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>functions = [
&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))
]
<b>输出:</b>{"t": 200, "resolved": [5]}
<b>解释:</b>
promiseAll(functions).then(console.log); // [5]
单个函数在 200 毫秒后以值 5 成功解析。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>functions = [
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)),
() =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject("Error"), 100))
]
<b>输出:</b>{"t": 100, "rejected": "Error"}
<b>解释:</b>由于其中一个 promise 被拒绝,返回的 promise 也在同一时间被拒绝并返回相同的错误。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>functions = [
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)),
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)),
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))
]
<b>输出:</b>{"t": 150, "resolved": [4, 10, 16]}
<b>解释:</b>所有的 promise 都成功执行。当最后一个 promise 被解析时,返回的 promise 也被解析了。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>functions&nbsp;is an array of functions that returns promises</code></li>
<li><code>1 &lt;= functions.length &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,45 @@
<p>现给定一个函数 <code>fn</code>&nbsp;,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code></p>
<p>在经过 <code>t</code> 毫秒的延迟后,<strong>除非</strong> 先调用 <code>cancelFn</code> ,否则&nbsp;<code>fn</code> 应该以 <code>args</code> 作为参数被调用。并且在这种情况下,<code>fn</code> 不应该被调用。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1:</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50
<b>输出:</b>[{"time": 20, "returned": 10}]
<b>解释:</b>
const cancel = cancellable(fn, [2], 20); // // 在 t=20ms 时调用 fn(2)
setTimeout(cancel, 50);
cancelTime50ms在延迟时间20ms之后所以 fn(2) 应该在 t=20ms 时调用。fn 的返回值是 10。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50
<b>输出:</b>[]
<b>解释:</b>fn(2) 从未被调用,因为 cancelTime50ms在延迟时间100ms之前。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100
<b>输出:</b>[{"time": 30, "returned": 8}]
<b>解释:</b>fn(2) 从未被调用,因为 cancelTime50ms在延迟时间100ms之前。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>fn 是一个函数</code></li>
<li><code>args 是一个有效的 JSON 数组</code></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>
</ul>

View File

@ -0,0 +1,45 @@
<p>给你一个仅由小写英文字母组成的字符串 <code>s</code> 。在一步操作中,你可以完成以下行为:</p>
<ul>
<li>选则 <code>s</code> 的任一非空子字符串,可能是整个字符串,接着将字符串中的每一个字符替换为英文字母表中的前一个字符。例如,'b' 用 'a' 替换,'a' 用 'z' 替换。</li>
</ul>
<p>返回执行上述操作 <strong>恰好一次</strong> 后可以获得的 <strong>字典序最小</strong> 的字符串。</p>
<p><strong>子字符串</strong> 是字符串中的一个连续字符序列。</p>
现有长度相同的两个字符串 <code>x</code> 和 字符串 <code>y</code> ,在满足&nbsp;<code>x[i] != y[i]</code> 的第一个位置 <code>i</code> 上,如果&nbsp; <code>x[i]</code> 在字母表中先于 <code>y[i]</code> 出现,则认为字符串 <code>x</code> 比字符串 <code>y</code> <strong>字典序更小</strong>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>s = "cbabc"
<strong>输出:</strong>"baabc"
<strong>解释:</strong>我们选择从下标 0 开始、到下标 1 结束的子字符串执行操作。
可以证明最终得到的字符串是字典序最小的。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s = "acbbc"
<strong>输出:</strong>"abaab"
<strong>解释:</strong>我们选择从下标 1 开始、到下标 4 结束的子字符串执行操作。
可以证明最终得到的字符串是字典序最小的。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s = "leetcode"
<strong>输出:</strong>"kddsbncd"
<strong>解释:</strong>我们选择整个字符串执行操作。
可以证明最终得到的字符串是字典序最小的。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> 仅由小写英文字母组成</li>
</ul>

View File

@ -0,0 +1,42 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>s</code>&nbsp;,这个字符串只包含&nbsp;<code>0</code>&nbsp;&nbsp;<code>9</code>&nbsp;的数字字符。</p>
<p>如果一个字符串&nbsp;<code>t</code>&nbsp;中至多有一对相邻字符是相等的,那么称这个字符串是 <strong>半重复的</strong>&nbsp;</p>
<p>请你返回 <code>s</code>&nbsp;中最长 <strong>半重复</strong>&nbsp;子字符串的长度。</p>
<p>一个 <strong>子字符串</strong>&nbsp;是一个字符串中一段连续 <strong>非空</strong>&nbsp;的字符。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>s = "52233"
<b>输出:</b>4
<b>解释:</b>最长半重复子字符串是 "5223" ,子字符串从 i = 0 开始,在 j = 3 处结束。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>s = "5494"
<b>输出:</b>4
<b>解释:</b>s 就是一个半重复字符串,所以答案为 4 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>s = "1111111"
<b>输出:</b>2
<b>解释:</b>最长半重复子字符串是 "11" ,子字符串从 i = 0 开始,在 j = 1 处结束。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>'0' &lt;= s[i] &lt;= '9'</code></li>
</ul>

View File

@ -0,0 +1,56 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始大小为&nbsp;<code>m x n</code>&nbsp;的二进制矩阵&nbsp;<code>grid</code>&nbsp;</p>
<p>从原矩阵中选出若干行构成一个行的 <strong>非空&nbsp;</strong>子集,如果子集中任何一列的和至多为子集大小的一半,那么我们称这个子集是 <strong>好子集</strong></p>
<p>更正式的,如果选出来的行子集大小(即行的数量)为 k那么每一列的和至多为&nbsp;<code>floor(k / 2)</code>&nbsp;</p>
<p>请你返回一个整数数组,它包含好子集的行下标,请你将子集中的元素&nbsp;<b>升序</b>&nbsp;返回。</p>
<p>如果有多个好子集,你可以返回任意一个。如果没有好子集,请你返回一个空数组。</p>
<p>一个矩阵 <code>grid</code>&nbsp;的行 <strong>子集</strong> ,是删除 <code>grid</code>&nbsp;中某些(也可能不删除)行后,剩余行构成的元素集合。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
<b>输出:</b>[0,1]
<b>解释:</b>我们可以选择第 0 和第 1 行构成一个好子集。
选出来的子集大小为 2 。
- 第 0&nbsp;列的和为 0 + 0 = 0 ,小于等于子集大小的一半。
- 第 1&nbsp;列的和为 1 + 0 = 1 ,小于等于子集大小的一半。
- 第 2&nbsp;列的和为 1 + 0 = 1 ,小于等于子集大小的一半。
- 第 3&nbsp;列的和为 0 + 1 = 1 ,小于等于子集大小的一半。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>grid = [[0]]
<b>输出:</b>[0]
<strong>解释:</strong>我们可以选择第 0 行构成一个好子集。
选出来的子集大小为 1 。
- 第 0 列的和为 0 ,小于等于子集大小的一半。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>grid = [[1,1,1],[1,1,1]]
<b>输出:</b>[]
<b>解释:</b>没有办法得到一个好子集。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= n &lt;= 5</code></li>
<li><code>grid[i][j]</code>&nbsp;要么是&nbsp;<code>0</code>&nbsp;,要么是&nbsp;<code>1</code></li>
</ul>

View File

@ -0,0 +1,39 @@
<p>给定一个数组 <code>arr</code> 和一个函数 <code>fn</code>,返回一个排序后的数组 <code>sortedArr</code>。你可以假设 <code>fn</code> 只返回数字,并且这些数字决定了 <code>sortedArr</code> 的排序顺序。<code>sortedArr</code> 必须按照 <code>fn</code> 的输出值&nbsp;<strong>升序</strong> 排序。</p>
<p>你可以假设对于给定的数组,<code>fn</code> 不会返回重复的数字。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>arr = [5, 4, 1, 2, 3], fn = (x) =&gt; x
<b>输出:</b>[1, 2, 3, 4, 5]
<b>解释:</b>fn 只是返回传入的数字,因此数组按升序排序。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>arr = [{"x": 1}, {"x": 0}, {"x": -1}], fn = (d) =&gt; d.x
<b>输出:</b>[{"x": -1}, {"x": 0}, {"x": 1}]
<b>解释:</b>fn 返回 "x" 键的值,因此数组根据该值排序。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>arr = [[3, 4], [5, 2], [10, 1]], fn = (x) =&gt; x[1]
<b>输出:</b>[[10, 1], [5, 2], [3, 4]]
<b>解释:</b>数组按照索引为 1 处的数字升序排序。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>arr 是一个有效的 JSON 数组</code></li>
<li><code>fn 是一个函数,返回一个数字</code></li>
<li><code>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>
</ul>

View File

@ -0,0 +1,40 @@
<p>给你一个长度为 <code>n</code> 、下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,表示收集不同巧克力的成本。每个巧克力都对应一个不同的类型,最初,位于下标 <code>i</code> 的巧克力就对应第 <code>i</code> 个类型。</p>
<p>在一步操作中,你可以用成本 <code>x</code> 执行下述行为:</p>
<ul>
<li>同时修改所有巧克力的类型,将巧克力的类型&nbsp;<code>i<sup>th</sup></code>&nbsp;修改为类型&nbsp;<code>((i + 1) mod n)<sup>th</sup></code></li>
</ul>
<p>假设你可以执行任意次操作,请返回收集所有类型巧克力所需的最小成本。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [20,1,15], x = 5
<strong>输出:</strong>13
<strong>解释:</strong>最开始,巧克力的类型分别是 [0,1,2] 。我们可以用成本 1 购买第 1 个类型的巧克力。
接着,我们用成本 5 执行一次操作,巧克力的类型变更为 [1,2,0] 。我们可以用成本 1 购买第 2 个类型的巧克力。
然后,我们用成本 5 执行一次操作,巧克力的类型变更为 [2,0,1] 。我们可以用成本 1 购买第 0 个类型的巧克力。
因此,收集所有类型的巧克力需要的总成本是 (1 + 5 + 1 + 5 + 1) = 13 。可以证明这是一种最优方案。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3], x = 4
<strong>输出:</strong>6
<strong>解释:</strong>我们将会按最初的成本收集全部三个类型的巧克力,而不需执行任何操作。因此,收集所有类型的巧克力需要的总成本是 1 + 2 + 3 = 6 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,36 @@
<p>给你一个整数数组 <code>nums</code> ,数组由 <strong>不同正整数</strong> 组成,请你找出并返回数组中 <strong>任一</strong> 既不是 <strong>最小值</strong> 也不是 <strong>最大值</strong> 的数字,如果不存在这样的数字,返回 <strong><code>-1</code></strong></p>
<p>返回所选整数。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [3,2,1,4]
<strong>输出:</strong>2
<strong>解释:</strong>在这个示例中,最小值是 1 ,最大值是 4 。因此2 或 3 都是有效答案。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [1,2]
<strong>输出:</strong>-1
<strong>解释:</strong>由于不存在既不是最大值也不是最小值的数字,我们无法选出满足题目给定条件的数字。因此,不存在答案,返回 -1 。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>nums = [2,1,3]
<strong>输出:</strong>2
<strong>解释:</strong>2 既不是最小值,也不是最大值,这个示例只有这一个有效答案。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li><code>nums</code> 中的所有数字互不相同</li>
</ul>

View File

@ -0,0 +1,48 @@
<p>给你两个长度为 <code>n</code> 、下标从 <strong>0</strong> 开始的整数数组 <code>nums1</code><code>nums2</code> ,另给你一个下标从 <strong>1</strong> 开始的二维数组 <code>queries</code> ,其中 <code>queries[i] = [x<sub>i</sub>, y<sub>i</sub>]</code></p>
<p>对于第 <code>i</code> 个查询,在所有满足 <code>nums1[j] &gt;= x<sub>i</sub></code><code>nums2[j] &gt;= y<sub>i</sub></code> 的下标 <code>j</code> <code>(0 &lt;= j &lt; n)</code> 中,找出 <code>nums1[j] + nums2[j]</code><strong>最大值</strong> ,如果不存在满足条件的 <code>j</code> 则返回 <strong>-1</strong></p>
<p>返回数组<em> </em><code>answer</code><em> </em>其中<em> </em><code>answer[i]</code><em> </em>是第 <code>i</code> 个查询的答案。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]
<strong>输出:</strong>[6,10,7]
<strong>解释:</strong>
对于第 1 个查询:<code>x<sub>i</sub> = 4</code>&nbsp;&nbsp;<code>y<sub>i</sub> = 1</code> ,可以选择下标&nbsp;<code>j = 0</code>&nbsp;,此时 <code>nums1[j] &gt;= 4</code>&nbsp;&nbsp;<code>nums2[j] &gt;= 1</code><code>nums1[j] + nums2[j]</code>&nbsp;等于 6 ,可以证明 6 是可以获得的最大值。
对于第 2 个查询:<code>x<sub>i</sub> = 1</code>&nbsp;&nbsp;<code>y<sub>i</sub> = 3</code> ,可以选择下标&nbsp;<code>j = 2</code>&nbsp;,此时 <code>nums1[j] &gt;= 1</code>&nbsp;&nbsp;<code>nums2[j] &gt;= 3</code><code>nums1[j] + nums2[j]</code>&nbsp;等于 10 ,可以证明 10 是可以获得的最大值。
对于第 3 个查询:<code>x<sub>i</sub> = 2</code>&nbsp;&nbsp;<code>y<sub>i</sub> = 5</code> ,可以选择下标&nbsp;<code>j = 3</code>&nbsp;,此时 <code>nums1[j] &gt;= 2</code>&nbsp;&nbsp;<code>nums2[j] &gt;= 5</code><code>nums1[j] + nums2[j]</code>&nbsp;等于 7 ,可以证明 7 是可以获得的最大值。
因此,我们返回&nbsp;<code>[6,10,7]</code>
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]
<strong>输出:</strong>[9,9,9]
<strong>解释:</strong>对于这个示例,我们可以选择下标&nbsp;<code>j = 2</code>&nbsp;,该下标可以满足每个查询的限制。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]
<strong>输出:</strong>[-1]
<strong>解释:</strong>示例中的查询 <code>x<sub>i</sub></code> = 3 且 <code>y<sub>i</sub></code> = 3 。对于每个下标 j ,都只满足 nums1[j] &lt; <code>x<sub>i</sub></code> 或者 nums2[j] &lt; <code>y<sub>i</sub></code> 。因此,不存在答案。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>nums1.length == nums2.length</code>&nbsp;</li>
<li><code>n ==&nbsp;nums1.length&nbsp;</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup>&nbsp;</code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i].length ==&nbsp;2</code></li>
<li><code>x<sub>i</sub>&nbsp;== queries[i][1]</code></li>
<li><code>y<sub>i</sub> == queries[i][2]</code></li>
<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,42 @@
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ,重复执行下述操作 <strong>任意</strong> 次:</p>
<ul>
<li>在字符串中选出一个下标 <code>i</code> ,并使 <code>c</code> 为字符串下标 <code>i</code> 处的字符。并在 <code>i</code> <strong>左侧</strong>(如果有)和 <strong>右侧</strong>(如果有)各 <strong>删除 </strong>一个距离 <code>i</code> <strong>最近</strong> 的字符 <code>c</code></li>
</ul>
<p>请你通过执行上述操作任意次,使 <code>s</code> 的长度 <strong>最小化</strong></p>
<p>返回一个表示 <strong>最小化</strong> 字符串的长度的整数。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "aaabc"
<strong>输出:</strong>3
<strong>解释:</strong>在这个示例中s 等于 "aaabc" 。我们可以选择位于下标 1 处的字符 'a' 开始。接着删除下标 1 左侧最近的那个 'a'(位于下标 0以及下标 1 右侧最近的那个 'a'(位于下标 2。执行操作后字符串变为 "abc" 。继续对字符串执行任何操作都不会改变其长度。因此,最小化字符串的长度是 3 。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "cbbd"
<strong>输出:</strong>3
<strong>解释:</strong>我们可以选择位于下标 1 处的字符 'b' 开始。下标 1 左侧不存在字符 'b' ,但右侧存在一个字符 'b'(位于下标 2所以会删除位于下标 2 的字符 'b' 。执行操作后,字符串变为 "cbd" 。继续对字符串执行任何操作都不会改变其长度。因此,最小化字符串的长度是 3 。</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>s = "dddaaa"
<strong>输出:</strong>2
<strong>解释:</strong>我们可以选择位于下标 1 处的字符 'd' 开始。接着删除下标 1 左侧最近的那个 'd'(位于下标 0以及下标 1 右侧最近的那个 'd'(位于下标 2。执行操作后字符串变为 "daaa" 。继续对新字符串执行操作,可以选择位于下标 2 的字符 'a' 。接着删除下标 2 左侧最近的那个 'a'(位于下标 1以及下标 2 右侧最近的那个 'a'(位于下标 3。执行操作后字符串变为 "da" 。继续对字符串执行任何操作都不会改变其长度。因此,最小化字符串的长度是 2 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> 仅由小写英文字母组成</li>
</ul>

View File

@ -0,0 +1,43 @@
<p>给你一个整数&nbsp;<code>n</code>&nbsp;和一个下标从 <strong>0</strong>&nbsp;开始的 <strong>二维数组</strong>&nbsp;<code>queries</code>&nbsp;,其中&nbsp;<code>queries[i] = [type<sub>i</sub>, index<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;</p>
<p>一开始,给你一个下标从 <strong>0</strong>&nbsp;开始的&nbsp;<code>n x n</code>&nbsp;矩阵,所有元素均为 <code>0</code>&nbsp;。每一个查询,你需要执行以下操作之一:</p>
<ul>
<li>如果&nbsp;<code>type<sub>i</sub> == 0</code>&nbsp;,将第&nbsp;<code>index<sub>i</sub></code>&nbsp;行的元素全部修改为&nbsp;<code>val<sub>i</sub></code>&nbsp;,覆盖任何之前的值。</li>
<li>如果&nbsp;<code>type<sub>i</sub> == 1</code>&nbsp;,将第&nbsp;<code>index<sub>i</sub></code>&nbsp;列的元素全部修改为 <code>val<sub>i</sub></code>&nbsp;,覆盖任何之前的值。</li>
</ul>
<p>请你执行完所有查询以后,返回矩阵中所有整数的和。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm1.png" style="width: 681px; height: 161px;"></p>
<pre><b>输入:</b>n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
<b>输出:</b>23
<b>解释:</b>上图展示了每个查询以后矩阵的值。所有操作执行完以后,矩阵元素之和为 23 。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm2.png" style="width: 681px; height: 331px;"></p>
<pre><b>输入:</b>n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
<b>输出:</b>17
<b>解释:</b>上图展示了每一个查询操作之后的矩阵。所有操作执行完以后,矩阵元素之和为 17 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>queries[i].length == 3</code></li>
<li><code>0 &lt;= type<sub>i</sub> &lt;= 1</code></li>
<li><code>0 &lt;= index<sub>i</sub>&nbsp;&lt; n</code></li>
<li><code>0 &lt;= val<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@ -0,0 +1,79 @@
<p>现给定两个数组 <code>arr1</code><code>arr2</code> ,返回一个新的数组 <code>joinedArray</code> 。两个输入数组中的每个对象都包含一个 <code>id</code> 字段。<code>joinedArray</code> 是一个通过 <code>id</code><code>arr1</code><code>arr2</code> 连接而成的数组。<code>joinedArray</code> 的长度应为唯一值 <code>id</code> 的长度。返回的数组应按 <code>id</code> <strong>升序</strong> 排序。</p>
<p>如果一个 <code>id</code> 存在于一个数组中但不存在于另一个数组中,则该对象应包含在结果数组中且不进行修改。</p>
<p>如果两个对象共享一个 <code>id</code> ,则它们的属性应进行合并:</p>
<ul>
<li>如果一个键只存在于一个对象中,则该键值对应该包含在对象中。</li>
<li>如果一个键在两个对象中都包含,则 <code>arr2</code> 中的值应覆盖 <code>arr1</code> 中的值。</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>
arr1 = [
&nbsp; {"id": 1, "x": 1},
&nbsp; {"id": 2, "x": 9}
],
arr2 = [
{"id": 3, "x": 5}
]
<b>输出:</b>
[
&nbsp; {"id": 1, "x": 1},
&nbsp; {"id": 2, "x": 9},
{"id": 3, "x": 5}
]
<b>解释:</b>没有共同的 id因此将 arr1 与 arr2 简单地连接起来。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>
arr1 = [
{"id": 1, "x": 2, "y": 3},
{"id": 2, "x": 3, "y": 6}
],
arr2 = [
{"id": 2, "x": 10, "y": 20},
{"id": 3, "x": 0, "y": 0}
]
<b>输出:</b>
[
{"id": 1, "x": 2, "y": 3},
{"id": 2, "x": 10, "y": 20},
&nbsp; {"id": 3, "x": 0, "y": 0}
]
<b>解释:</b>id 为 1 和 id 为 3 的对象在结果数组中保持不变。id 为 2 的两个对象合并在一起。arr2 中的键覆盖 arr1 中的值。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>
arr1 = [
{"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}
]
arr2 = [
{"id": 1, "b": {"c": 84}, "v": [1, 3]}
]
<strong>输出:</strong> [
{"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
]
<b>解释:</b>具有 id 为 1 的对象合并在一起。对于键 "b" 和 "v" ,使用 arr2 中的值。由于键 "y" 只存在于 arr1 中,因此取 arr1 的值。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>arr1 和 arr2 都是有效的 JSON 数组</code></li>
<li><code>在 arr1 和 arr2 中都有唯一的键值 id</code></li>
<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>
<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@ -0,0 +1,30 @@
给定两个 promise 对象&nbsp;<code>promise1</code><code>promise2</code>,返回一个新的 promise。<code>promise1</code><code>promise2</code> 都会被解析为一个数字。返回的 Promise 应该解析为这两个数字的和。
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>
promise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(2), 20)),
promise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 60))
<b>输出:</b>7
<b>解释:</b>两个输入的 Promise 分别解析为值 2 和 5。返回的 Promise 应该解析为 2 + 5 = 7。返回的 Promise 解析的时间不作为判断条件。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>
promise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 50)),
promise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(-12), 30))
<b>输出:</b>-2
<b>解释:</b>两个输入的 Promise 分别解析为值 10 和 -12。返回的 Promise 应该解析为 10 + -12 = -2。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>promise1 和 promise2 都是被解析为一个数字的 promise 对象</code></li>
</ul>

View File

@ -0,0 +1,67 @@
<p>有一些机器人分布在一条无限长的数轴上,他们初始坐标用一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;表示。当你给机器人下达命令时,它们以每秒钟一单位的速度开始移动。</p>
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;,每个字符按顺序分别表示每个机器人移动的方向。<code>'L'</code>&nbsp;表示机器人往左或者数轴的负方向移动,<code>'R'</code>&nbsp;表示机器人往右或者数轴的正方向移动。</p>
<p>当两个机器人相撞时,它们开始沿着原本相反的方向移动。</p>
<p>请你返回指令重复执行 <code>d</code>&nbsp;秒后,所有机器人之间两两距离之和。由于答案可能很大,请你将答案对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;取余后返回。</p>
<p><b>注意:</b></p>
<ul>
<li>对于坐标在&nbsp;<code>i</code>&nbsp;<code>j</code>&nbsp;的两个机器人,<code>(i,j)</code>&nbsp;&nbsp;<code>(j,i)</code>&nbsp;视为相同的坐标对。也就是说,机器人视为无差别的。</li>
<li>当机器人相撞时,它们 <strong>立即改变</strong>&nbsp;它们的前进时间,这个过程不消耗任何时间。</li>
<li>
<p>当两个机器人在同一时刻占据相同的位置时,就会相撞。</p>
<ul>
<li>
<p>例如,如果一个机器人位于位置 0 并往右移动,另一个机器人位于位置 2 并往左移动,下一秒,它们都将占据位置 1并改变方向。再下一秒钟后第一个机器人位于位置 0 并往左移动,而另一个机器人位于位置 2 并往右移动。</p>
</li>
<li>
<p>例如,如果一个机器人位于位置 0 并往右移动,另一个机器人位于位置 1 并往左移动,下一秒,第一个机器人位于位置 0 并往左行驶,而另一个机器人位于位置 1 并往右移动。</p>
</li>
</ul>
</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [-2,0,2], s = "RLL", d = 3
<b>输出:</b>8
<b>解释:</b>
1 秒后,机器人的位置为 [-1,-1,1] 。现在下标为 0 的机器人开始往左移动,下标为 1 的机器人开始往右移动。
2 秒后,机器人的位置为 [-2,0,0] 。现在下标为 1 的机器人开始往左移动,下标为 2 的机器人开始往右移动。
3 秒后,机器人的位置为 [-3,-1,1] 。
下标为 0 和 1 的机器人之间距离为 abs(-3 - (-1)) = 2 。
下标为 0 和 2 的机器人之间的距离为 abs(-3 - 1) = 4 。
下标为 1 和 2 的机器人之间的距离为 abs(-1 - 1) = 2 。
所有机器人对之间的总距离为 2 + 4 + 2 = 8 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [1,0], s = "RL", d = 2
<b>输出:</b>5
<b>解释:</b>
1 秒后,机器人的位置为 [2,-1] 。
2 秒后,机器人的位置为 [3,-2] 。
两个机器人的距离为 abs(-2 - 3) = 5 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-2 * 10<sup>9</sup>&nbsp;&lt;= nums[i] &lt;= 2 * 10<sup>9</sup></code></li>
<li><code>0 &lt;= d &lt;= 10<sup>9</sup></code></li>
<li><code>nums.length == s.length&nbsp;</code></li>
<li><code>s</code>&nbsp;只包含&nbsp;<code>'L'</code><code>'R'</code>&nbsp;</li>
<li><code>nums[i]</code>&nbsp;互不相同。</li>
</ul>

View File

@ -0,0 +1,37 @@
<p>给你两个数字字符串&nbsp;<code>num1</code>&nbsp;&nbsp;<code>num2</code>&nbsp;,以及两个整数&nbsp;<code>max_sum</code>&nbsp;<code>min_sum</code>&nbsp;。如果一个整数&nbsp;<code>x</code>&nbsp;满足以下条件,我们称它是一个好整数:</p>
<ul>
<li><code>num1 &lt;= x &lt;= num2</code></li>
<li><code>min_sum &lt;= digit_sum(x) &lt;= max_sum</code>.</li>
</ul>
<p>请你返回好整数的数目。答案可能很大,请返回答案对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;取余后的结果。</p>
<p>注意,<code>digit_sum(x)</code>&nbsp;表示&nbsp;<code>x</code>&nbsp;各位数字之和。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>num1 = "1", num2 = "12", min_num = 1, max_num = 8
<b>输出:</b>11
<b>解释:</b>总共有 11 个整数的数位和在 1 到 8 之间,分别是 1,2,3,4,5,6,7,8,10,11 和 12 。所以我们返回 11 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>num1 = "1", num2 = "5", min_num = 1, max_num = 5
<b>输出:</b>5
<b>解释:</b>数位和在 1 到 5 之间的 5 个整数分别为 1,2,3,4 和 5 。所以我们返回 5 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num1 &lt;= num2 &lt;= 10<sup>22</sup></code></li>
<li><code>1 &lt;= min_sum &lt;= max_sum &lt;= 400</code></li>
</ul>

View File

@ -0,0 +1,79 @@
现给定一个函数 <code>fn</code>,一个参数数组 <code>args</code> 和一个时间间隔 <code>t</code>,返回一个取消函数 <code>cancelFn</code>。函数 <code>fn</code> 应该立即使用 <code>args</code> 调用,并且在每个 <code>t</code> 毫秒内再次调用,直到调用 <code>cancelFn</code>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x * 2, args = [4], t = 20, cancelT = 110
<b>输出:</b>
[
{"time": 0, "returned": 8},
{"time": 20, "returned": 8},
{"time": 40, "returned": 8},
{"time": 60, "returned": 8},
{"time": 80, "returned": 8},
{"time": 100, "returned": 8}
]
<strong>解释:</strong> 每隔 20ms调用 fn(4)。直到 t=110ms然后取消。
const cancel = cancellable(x =&gt; x * 2, [4], 20);
setTimeout(cancel, 110);
第一次调用 fn 是在 0ms。fn(4) 返回 8。
第二次调用 fn 是在 20ms。fn(4) 返回 8。
第三次调用 fn 是在 40ms。fn(4) 返回 8。
第四次调用 fn 是在&nbsp;60ms。fn(4) 返回 8。
第五次调用 fn 是在 80ms。fn(4) 返回 8。
第六次调用 fn 是在 100ms。fn(4) 返回 8。</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 25, cancelT = 140
<strong>输出:</strong>
[
{"time": 0, "returned": 10},
{"time": 25, "returned": 10},
{"time": 50, "returned": 10},
{"time": 75, "returned": 10},
{"time": 100, "returned": 10},
{"time": 125, "returned": 10}
]
<strong>解释:</strong>每隔 25ms调用 fn(2, 5)。直到 t=140ms然后取消。
第一次调用 fn 是在 0ms
第二次调用 fn 是在 25ms
第三次调用 fn 是在 50ms
第四次调用 fn 是在&nbsp;75ms
第五次调用 fn 是在 100ms
第六次调用 fn 是在 125ms
在 140ms 取消
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180
<b>输出:</b>
[
{"time": 0, "returned": 9},
{"time": 50, "returned": 9},
{"time": 100, "returned": 9},
{"time": 150, "returned": 9}
]
<b>解释:</b>每隔 50ms调用 fn(5, 1, 3)。直到 t=180ms然后取消。
第一次调用 fn 是在 0ms
第二次调用 fn 是在 50ms
第三次调用 fn 是在 100ms
第四次调用 fn 是在&nbsp;150ms
在 180ms 取消
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>fn 是一个函数</code></li>
<li><code>args 是一个有效的 JSON 数组</code></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>
</ul>

View File

@ -0,0 +1,55 @@
<p>Design a <code>Calculator</code> class. The class should provide the&nbsp;mathematical operations of&nbsp;addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining.&nbsp;The <code>Calculator</code> class constructor should accept a number&nbsp;which serves as the&nbsp;initial value of <code>result</code>.</p>
<p>Your <font face="monospace"><code>Calculator</code>&nbsp;</font>class should have the following methods:</p>
<ul>
<li><code>add</code> - This method adds the given number <code>value</code> to the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>
<li><code>subtract</code> -&nbsp;This method subtracts the given number <code>value</code>&nbsp;from the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>
<li><code>multiply</code> -&nbsp;This method multiplies the <code>result</code>&nbsp; by the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>
<li><code>divide</code> -&nbsp;This method divides the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>. If the passed value is <code>0</code>, an error <code>&quot;Division by zero is not allowed&quot;</code> should be thrown.</li>
<li><code>power</code> -&nbsp;This method raises the&nbsp;<code>result</code> to the power of the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>
<li><code>getResult</code> -&nbsp;This method returns the <code>result</code>.</li>
</ul>
<p>Solutions within&nbsp;<code>10<sup>-5</sup></code>&nbsp;of the actual result are considered correct.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;getResult&quot;], values = [10, 5, 7]
<strong>Output:</strong> 8
<strong>Explanation:</strong>
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;multiply&quot;, &quot;power&quot;, &quot;getResult&quot;], values = [2, 5, 2]
<strong>Output:</strong> 100
<strong>Explanation:</strong>
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;divide&quot;, &quot;getResult&quot;], values = [20, 0]
<strong>Output:</strong> &quot;Division by zero is not allowed&quot;
<strong>Explanation:</strong>
new Calculator(20).divide(0).getResult() // 20 / 0
The error should be thrown because we cannot divide by zero.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup></code><code>&nbsp;- 1</code></li>
<li><code>actions[i] is one of &quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;multiply&quot;, &quot;divide&quot;, &quot;power&quot;, and&nbsp;&quot;getResult&quot;</code></li>
<li><code><font face="monospace">Last action is always &quot;getResult&quot;</font></code></li>
<li><code><font face="monospace">values is a JSON array of numbers</font></code></li>
</ul>

View File

@ -0,0 +1,35 @@
<p>You are given an integer <code>n</code> that consists of exactly <code>3</code> digits.</p>
<p>We call the number <code>n</code> <strong>fascinating</strong> if, after the following modification, the resulting number contains all the digits from <code>1</code> to <code>9</code> <strong>exactly</strong> once and does not contain any <code>0</code>&#39;s:</p>
<ul>
<li><strong>Concatenate</strong> <code>n</code> with the numbers <code>2 * n</code> and <code>3 * n</code>.</li>
</ul>
<p>Return <code>true</code><em> if </em><code>n</code><em> is fascinating, or </em><code>false</code><em> otherwise</em>.</p>
<p><strong>Concatenating</strong> two numbers means joining them together. For example, the concatenation of <code>121</code> and <code>371</code> is <code>121371</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 192
<strong>Output:</strong> true
<strong>Explanation:</strong> We concatenate the numbers n = 192 and 2 * n = 384 and 3 * n = 576. The resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 100
<strong>Output:</strong> false
<strong>Explanation:</strong> We concatenate the numbers n = 100 and 2 * n = 200 and 3 * n = 300. The resulting number is 100200300. This number does not satisfy any of the conditions.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>100 &lt;= n &lt;= 999</code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>Given an object or an array, return if it is empty.</p>
<ul>
<li>An empty object contains no key-value pairs.</li>
<li>An empty array contains no elements.</li>
</ul>
<p>You may assume the object or array is the output of&nbsp;<code>JSON.parse</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> obj = {&quot;x&quot;: 5, &quot;y&quot;: 42}
<strong>Output:</strong> false
<strong>Explanation:</strong> The object has 2 key-value pairs so it is not empty.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> obj = {}
<strong>Output:</strong> true
<strong>Explanation:</strong> The object doesn&#39;t have any key-value pairs so it is empty.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> obj = [null, false, 0]
<strong>Output:</strong> false
<strong>Explanation:</strong> The array has 3 elements so it is not empty.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>&nbsp;<code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>
</ul>
<p>&nbsp;</p>
<strong>Can you solve it in O(1) time?</strong>

View File

@ -0,0 +1,52 @@
<p>You are given a <strong>0-indexed</strong> permutation of <code>n</code> integers <code>nums</code>.</p>
<p>A permutation is called <strong>semi-ordered</strong> if the first number equals <code>1</code> and the last number equals <code>n</code>. You can perform the below operation as many times as you want until you make <code>nums</code> a <strong>semi-ordered</strong> permutation:</p>
<ul>
<li>Pick two adjacent elements in <code>nums</code>, then swap them.</li>
</ul>
<p>Return <em>the minimum number of operations to make </em><code>nums</code><em> a <strong>semi-ordered permutation</strong></em>.</p>
<p>A <strong>permutation</strong> is a sequence of integers from <code>1</code> to <code>n</code> of length <code>n</code> containing each number exactly once.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,4,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:
1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,4,1,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can make the permutation semi-ordered using these sequence of operations:
1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3].
2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,4,2,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The permutation is already a semi-ordered permutation.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= nums.length == n &lt;= 50</code></li>
<li><code>1 &lt;= nums[i]&nbsp;&lt;= 50</code></li>
<li><code>nums is a permutation.</code></li>
</ul>

View File

@ -0,0 +1,60 @@
<p>Given an array of asynchronous&nbsp;functions&nbsp;<code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments&nbsp;and returns a promise.</p>
<p><code>promise</code> resolves:</p>
<ul>
<li>When all the promises returned from&nbsp;<code>functions</code>&nbsp;were resolved successfully.&nbsp;The resolved&nbsp;value of&nbsp;<code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the&nbsp;<code>functions.</code></li>
</ul>
<p><code>promise</code> rejects:</p>
<ul>
<li>When any&nbsp;of the promises&nbsp;returned from&nbsp;<code>functions</code>&nbsp;were rejected.&nbsp;<code>promise</code> should also&nbsp;reject&nbsp;with the reason of the first rejection.</li>
</ul>
<p>Please solve it without using the built-in&nbsp;<code>Promise.all</code>&nbsp;function.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> functions = [
&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))
]
<strong>Output:</strong> {&quot;t&quot;: 200, &quot;resolved&quot;: [5]}
<strong>Explanation:</strong>
promiseAll(functions).then(console.log); // [5]
The single function was resolved at 200ms with a value of 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> functions = [
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)),
() =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(&quot;Error&quot;), 100))
]
<strong>Output:</strong> {&quot;t&quot;: 100, &quot;rejected&quot;: &quot;Error&quot;}
<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> functions = [
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)),
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)),
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))
]
<strong>Output:</strong> {&quot;t&quot;: 150, &quot;resolved&quot;: [4, 10, 16]}
<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>functions&nbsp;is an array of functions that returns promises</code></li>
<li><code>1 &lt;= functions.length &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>Given a function <code>fn</code>, an array or 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&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case,&nbsp;<code>fn</code> should never be called.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
<strong>Explanation:</strong>
const cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms
setTimeout(cancel, 50);
the cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50
<strong>Output:</strong> []
<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime &gt; t.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>fn is a function</code></li>
<li><code>args is a valid JSON array</code></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>
</ul>

View File

@ -0,0 +1,45 @@
<p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can do the following:</p>
<ul>
<li>Select any non-empty substring of <code>s</code>, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, &#39;b&#39; is converted to &#39;a&#39;, and &#39;a&#39; is converted to &#39;z&#39;.</li>
</ul>
<p>Return <em>the <strong>lexicographically smallest</strong> string you can obtain after performing the above operation <strong>exactly once</strong>.</em></p>
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
A string <code>x</code> is <strong>lexicographically smaller</strong> than a string <code>y</code> of the same length if <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order for the first position <code>i</code> such that <code>x[i] != y[i]</code>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;cbabc&quot;
<strong>Output:</strong> &quot;baabc&quot;
<strong>Explanation:</strong> We apply the operation on the substring starting at index 0, and ending at index 1 inclusive.
It can be proven that the resulting string is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;acbbc&quot;
<strong>Output:</strong> &quot;abaab&quot;
<strong>Explanation:</strong> We apply the operation on the substring starting at index 1, and ending at index 4 inclusive.
It can be proven that the resulting string is the lexicographically smallest.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;leetcode&quot;
<strong>Output:</strong> &quot;kddsbncd&quot;
<strong>Explanation:</strong> We apply the operation on the entire string.
It can be proven that the resulting string is the lexicographically smallest.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of lowercase English letters</li>
</ul>

View File

@ -0,0 +1,40 @@
<p>You are given a <strong>0-indexed</strong> string <code>s</code> that consists of digits from <code>0</code> to <code>9</code>.</p>
<p>A string <code>t</code> is called a <strong>semi-repetitive</strong> if there is at most one consecutive pair of the same digits inside <code>t</code>. For example, <code>0010</code>, <code>002020</code>, <code>0123</code>, <code>2002</code>, and <code>54944</code> are semi-repetitive while&nbsp;<code>00101022</code>, and <code>1101234883</code> are not.</p>
<p>Return <em>the length of the longest semi-repetitive substring inside</em> <code>s</code>.</p>
<p>A <b>substring</b> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;52233&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest semi-repetitive substring is &quot;5223&quot;, which starts at i = 0 and ends at j = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;5494&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> s is a semi-reptitive string, so the answer is 4.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;1111111&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest semi-repetitive substring is &quot;11&quot;, which starts at i = 0 and ends at j = 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>&#39;0&#39; &lt;= s[i] &lt;= &#39;9&#39;</code></li>
</ul>

View File

@ -0,0 +1,54 @@
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p>
<p>Let us call a <strong>non-empty</strong> subset of rows <strong>good</strong> if the sum of each column of the subset is at most half of the length of the subset.</p>
<p>More formally, if the length of the chosen subset of rows is <code>k</code>, then the sum of each column should be at most <code>floor(k / 2)</code>.</p>
<p>Return <em>an integer array that contains row indices of a good subset sorted in <strong>ascending</strong> order.</em></p>
<p>If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array.</p>
<p>A <strong>subset</strong> of rows of the matrix <code>grid</code> is any matrix that can be obtained by deleting some (possibly none or all) rows from <code>grid</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
<strong>Output:</strong> [0,1]
<strong>Explanation:</strong> We can choose the 0<sup>th</sup> and 1<sup>st</sup> rows to create a good subset of rows.
The length of the chosen subset is 2.
- The sum of the 0<sup>th</sup>&nbsp;column is 0 + 0 = 0, which is at most half of the length of the subset.
- The sum of the 1<sup>st</sup>&nbsp;column is 1 + 0 = 1, which is at most half of the length of the subset.
- The sum of the 2<sup>nd</sup>&nbsp;column is 1 + 0 = 1, which is at most half of the length of the subset.
- The sum of the 3<sup>rd</sup>&nbsp;column is 0 + 1 = 1, which is at most half of the length of the subset.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[0]]
<strong>Output:</strong> [0]
<strong>Explanation:</strong> We can choose the 0<sup>th</sup> row to create a good subset of rows.
The length of the chosen subset is 1.
- The sum of the 0<sup>th</sup>&nbsp;column is 0, which is at most half of the length of the subset.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,1,1],[1,1,1]]
<strong>Output:</strong> []
<strong>Explanation:</strong> It is impossible to choose any subset of rows to create a good subset.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= n &lt;= 5</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

View File

@ -0,0 +1,37 @@
<p>Given an array <code>arr</code> and a function <code>fn</code>, return a sorted array <code>sortedArr</code>. You can assume&nbsp;<code>fn</code>&nbsp;only returns numbers and those numbers determine the sort order of&nbsp;<code>sortedArr</code>. <code>sortedArray</code> must be sorted in <strong>ascending order</strong> by <code>fn</code> output.</p>
<p>You may assume that <code>fn</code> will never duplicate numbers for a given array.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [5, 4, 1, 2, 3], fn = (x) =&gt; x
<strong>Output:</strong> [1, 2, 3, 4, 5]
<strong>Explanation:</strong> fn simply returns the number passed to it so the array is sorted in ascending order.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [{&quot;x&quot;: 1}, {&quot;x&quot;: 0}, {&quot;x&quot;: -1}], fn = (d) =&gt; d.x
<strong>Output:</strong> [{&quot;x&quot;: -1}, {&quot;x&quot;: 0}, {&quot;x&quot;: 1}]
<strong>Explanation:</strong> fn returns the value for the &quot;x&quot; key. So the array is sorted based on that value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [[3, 4], [5, 2], [10, 1]], fn = (x) =&gt; x[1]
<strong>Output:</strong> [[10, 1], [5, 2], [3, 4]]
<strong>Explanation:</strong> arr is sorted in ascending order by number at index=1.&nbsp;
</pre>
<p>&nbsp;</p>
<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>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index <code>i</code>&nbsp;is <code>nums[i]</code>. Each chocolate is of a different type, and initially, the chocolate at the index&nbsp;<code>i</code>&nbsp;is of <code>i<sup>th</sup></code> type.</p>
<p>In one operation, you can do the following with an incurred <strong>cost</strong> of <code>x</code>:</p>
<ul>
<li>Simultaneously change the chocolate of <code>i<sup>th</sup></code> type to <code>((i + 1) mod n)<sup>th</sup></code> type for all chocolates.</li>
</ul>
<p>Return <em>the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [20,1,15], x = 5
<strong>Output:</strong> 13
<strong>Explanation:</strong> Initially, the chocolate types are [0,1,2]. We will buy the 1<sup>st</sup>&nbsp;type of chocolate at a cost of 1.
Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2<sup>nd</sup><sup> </sup>type of chocolate at a cost of 1.
Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0<sup>th </sup>type of chocolate at a cost of 1.
Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3], x = 4
<strong>Output:</strong> 6
<strong>Explanation:</strong> We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,37 @@
<p>Given an integer array <code>nums</code> containing <strong>distinct</strong> <strong>positive</strong> integers, find and return <strong>any</strong> number from the array that is neither the <strong>minimum</strong> nor the <strong>maximum</strong> value in the array, or <strong><code>-1</code></strong> if there is no such number.</p>
<p>Return <em>the selected integer.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li>All values in <code>nums</code> are distinct</li>
</ul>

View File

@ -0,0 +1,52 @@
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of length <code>n</code>, and a <strong>1-indexed 2D array</strong> <code>queries</code> where <code>queries[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
<p>For the <code>i<sup>th</sup></code> query, find the <strong>maximum value</strong> of <code>nums1[j] + nums2[j]</code> among all indices <code>j</code> <code>(0 &lt;= j &lt; n)</code>, where <code>nums1[j] &gt;= x<sub>i</sub></code> and <code>nums2[j] &gt;= y<sub>i</sub></code>, or <strong>-1</strong> if there is no <code>j</code> satisfying the constraints.</p>
<p>Return <em>an array </em><code>answer</code><em> where </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]
<strong>Output:</strong> [6,10,7]
<strong>Explanation:</strong>
For the 1st query <code node="[object Object]">x<sub>i</sub> = 4</code>&nbsp;and&nbsp;<code node="[object Object]">y<sub>i</sub> = 1</code>, we can select index&nbsp;<code node="[object Object]">j = 0</code>&nbsp;since&nbsp;<code node="[object Object]">nums1[j] &gt;= 4</code>&nbsp;and&nbsp;<code node="[object Object]">nums2[j] &gt;= 1</code>. The sum&nbsp;<code node="[object Object]">nums1[j] + nums2[j]</code>&nbsp;is 6, and we can show that 6 is the maximum we can obtain.
For the 2nd query <code node="[object Object]">x<sub>i</sub> = 1</code>&nbsp;and&nbsp;<code node="[object Object]">y<sub>i</sub> = 3</code>, we can select index&nbsp;<code node="[object Object]">j = 2</code>&nbsp;since&nbsp;<code node="[object Object]">nums1[j] &gt;= 1</code>&nbsp;and&nbsp;<code node="[object Object]">nums2[j] &gt;= 3</code>. The sum&nbsp;<code node="[object Object]">nums1[j] + nums2[j]</code>&nbsp;is 10, and we can show that 10 is the maximum we can obtain.
For the 3rd query <code node="[object Object]">x<sub>i</sub> = 2</code>&nbsp;and&nbsp;<code node="[object Object]">y<sub>i</sub> = 5</code>, we can select index&nbsp;<code node="[object Object]">j = 3</code>&nbsp;since&nbsp;<code node="[object Object]">nums1[j] &gt;= 2</code>&nbsp;and&nbsp;<code node="[object Object]">nums2[j] &gt;= 5</code>. The sum&nbsp;<code node="[object Object]">nums1[j] + nums2[j]</code>&nbsp;is 7, and we can show that 7 is the maximum we can obtain.
Therefore, we return&nbsp;<code node="[object Object]">[6,10,7]</code>.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]
<strong>Output:</strong> [9,9,9]
<strong>Explanation:</strong> For this example, we can use index&nbsp;<code node="[object Object]">j = 2</code>&nbsp;for all the queries since it satisfies the constraints for each query.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]
<strong>Output:</strong> [-1]
<strong>Explanation:</strong> There is one query in this example with <code node="[object Object]">x<sub>i</sub></code> = 3 and <code node="[object Object]">y<sub>i</sub></code> = 3. For every index, j, either nums1[j] &lt; <code node="[object Object]">x<sub>i</sub></code> or nums2[j] &lt; <code node="[object Object]">y<sub>i</sub></code>. Hence, there is no solution.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>nums1.length == nums2.length</code>&nbsp;</li>
<li><code>n ==&nbsp;nums1.length&nbsp;</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup>&nbsp;</code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i].length ==&nbsp;2</code></li>
<li><code>x<sub>i</sub>&nbsp;== queries[i][1]</code></li>
<li><code>y<sub>i</sub> == queries[i][2]</code></li>
<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>Given a <strong>0-indexed</strong> string <code>s</code>, repeatedly perform the following operation <strong>any</strong> number of times:</p>
<ul>
<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if any) and the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if any).</li>
</ul>
<p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operation any number of times.</p>
<p>Return <em>an integer denoting the length of the <strong>minimized</strong> string.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;aaabc&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> In this example, s is &quot;aaabc&quot;. We can start by selecting the character &#39;a&#39; at index 1. We then remove the closest &#39;a&#39; to the left of index 1, which is at index 0, and the closest &#39;a&#39; to the right of index 1, which is at index 2. After this operation, the string becomes &quot;abc&quot;. Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;cbbd&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> For this we can start with character &#39;b&#39; at index 1. There is no occurrence of &#39;b&#39; to the left of index 1, but there is one to the right at index 2, so we delete the &#39;b&#39; at index 2. The string becomes &quot;cbd&quot; and further operations will leave it unchanged. Hence, the minimized length is 3.&nbsp;
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;dddaaa&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> For this, we can start with the character &#39;d&#39; at index 1. The closest occurrence of a &#39;d&#39; to its left is at index 0, and the closest occurrence of a &#39;d&#39; to its right is at index 2. We delete both index 0 and 2, so the string becomes &quot;daaa&quot;. In the new string, we can select the character &#39;a&#39; at index 2. The closest occurrence of an &#39;a&#39; to its left is at index 1, and the closest occurrence of an &#39;a&#39; to its right is at index 3. We delete both of them, and the string becomes &quot;da&quot;. We cannot minimize this further, so the minimized length is 2.
</pre>
<div class="notranslate" style="all: initial;">&nbsp;</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> contains only lowercase English letters</li>
</ul>

View File

@ -0,0 +1,39 @@
<p>You are given an integer <code>n</code> and a <strong>0-indexed</strong>&nbsp;<strong>2D array</strong> <code>queries</code> where <code>queries[i] = [type<sub>i</sub>, index<sub>i</sub>, val<sub>i</sub>]</code>.</p>
<p>Initially, there is a <strong>0-indexed</strong> <code>n x n</code> matrix filled with <code>0</code>&#39;s. For each query, you must apply one of the following changes:</p>
<ul>
<li>if <code>type<sub>i</sub> == 0</code>, set the values in the row with <code>index<sub>i</sub></code> to <code>val<sub>i</sub></code>, overwriting any previous values.</li>
<li>if <code>type<sub>i</sub> == 1</code>, set the values in the column with <code>index<sub>i</sub></code> to <code>val<sub>i</sub></code>, overwriting any previous values.</li>
</ul>
<p>Return <em>the sum of integers in the matrix after all queries are applied</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm1.png" style="width: 681px; height: 161px;" />
<pre>
<strong>Input:</strong> n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
<strong>Output:</strong> 23
<strong>Explanation:</strong> The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 23.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm2.png" style="width: 681px; height: 331px;" />
<pre>
<strong>Input:</strong> n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
<strong>Output:</strong> 17
<strong>Explanation:</strong> The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 17.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>queries[i].length == 3</code></li>
<li><code>0 &lt;= type<sub>i</sub> &lt;= 1</code></li>
<li><code>0 &lt;= index<sub>i</sub>&nbsp;&lt; n</code></li>
<li><code>0 &lt;= val<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@ -0,0 +1,77 @@
<p>Given two arrays <code>arr1</code> and <code>arr2</code>, return a new&nbsp;array <code>joinedArray</code>. All the objects in each&nbsp;of the two inputs arrays will contain an&nbsp;<code>id</code>&nbsp;field that has an integer value.&nbsp;<code>joinedArray</code>&nbsp;is an array formed by merging&nbsp;<code>arr1</code> and <code>arr2</code> based on&nbsp;their <code>id</code>&nbsp;key. The length of&nbsp;<code>joinedArray</code> should be the length of unique values of <code>id</code>. The returned array should be sorted in&nbsp;<strong>ascending</strong>&nbsp;order based on the <code>id</code>&nbsp;key.</p>
<p>If a given&nbsp;<code>id</code>&nbsp;exists in one array but not the other, the single object with that&nbsp;<code>id</code> should be included in the result array without modification.</p>
<p>If two objects share an <code>id</code>, their properties should be merged into a single&nbsp;object:</p>
<ul>
<li>If a key only exists in one object, that single key-value pair should be included in the object.</li>
<li>If a key is included in both objects, the value in the object from <code>arr2</code>&nbsp;should override the value from <code>arr1</code>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong>
arr1 = [
&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},
&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9}
],
arr2 = [
{&quot;id&quot;: 3, &quot;x&quot;: 5}
]
<strong>Output:</strong>
[
&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},
&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9},
{&quot;id&quot;: 3, &quot;x&quot;: 5}
]
<strong>Explanation:</strong> There are no duplicate ids so arr1 is simply concatenated with arr2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong>
arr1 = [
{&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},
{&quot;id&quot;: 2, &quot;x&quot;: 3, &quot;y&quot;: 6}
],
arr2 = [
{&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},
{&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}
]
<strong>Output:</strong>
[
{&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},
{&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},
&nbsp; {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}
]
<strong>Explanation:</strong> The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong>
arr1 = [
{&quot;id&quot;: 1, &quot;b&quot;: {&quot;b&quot;: 94},&quot;v&quot;: [4, 3], &quot;y&quot;: 48}
]
arr2 = [
{&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3]}
]
<strong>Output:</strong> [
{&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3], &quot;y&quot;: 48}
]
<strong>Explanation:</strong> The two objects with id=1 are merged together. For the keys &quot;b&quot; and &quot;v&quot; the values from arr2 are used. Since the key &quot;y&quot; only exists in arr1, that value is taken form arr1.</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>arr1 and arr2 are valid JSON arrays</code></li>
<li><code>Each object in arr1 and arr2 has a unique&nbsp;integer id key</code></li>
<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>
<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@ -0,0 +1,28 @@
Given two promises <code>promise1</code> and <code>promise2</code>, return a new promise. <code>promise1</code> and <code>promise2</code>&nbsp;will both resolve with a number. The returned promise should resolve with the sum of the two numbers.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong>
promise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(2), 20)),
promise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 60))
<strong>Output:</strong> 7
<strong>Explanation:</strong> The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong>
promise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 50)),
promise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(-12), 30))
<strong>Output:</strong> -2
<strong>Explanation:</strong> The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>promise1 and promise2 are&nbsp;promises that resolve&nbsp;with a number</code></li>
</ul>

View File

@ -0,0 +1,59 @@
<p>Some robots are standing on an infinite number line with their initial coordinates given by a <strong>0-indexed</strong> integer array <code>nums</code> and will start moving once given the command to move. The robots will move a unit distance each second.</p>
<p>You are given a string <code>s</code> denoting the direction in which robots will move on command. <code>&#39;L&#39;</code> means the robot will move towards the left side or negative side of the number line, whereas <code>&#39;R&#39;</code> means the robot will move towards the right side or positive side of the number line.</p>
<p>If two robots collide, they will start moving in opposite directions.</p>
<p>Return <em>the sum of distances between all the&nbsp;pairs of robots </em><code>d</code> <em>seconds after&nbsp;the command. </em>Since the sum can be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
<p><b>Note: </b></p>
<ul>
<li>For two robots at the index <code>i</code> and <code>j</code>, pair <code>(i,j)</code> and pair <code>(j,i)</code> are considered the same pair.</li>
<li>When robots collide, they <strong>instantly change</strong> their directions without wasting any time.</li>
<li>Collision happens&nbsp;when two robots share the same place in a&nbsp;moment.
<ul>
<li>For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they&#39;ll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.</li>
<li>For example,&nbsp;if a robot is positioned in 0 going to the right and another is positioned in 1&nbsp;going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.</li>
</ul>
</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,2], s = &quot;RLL&quot;, d = 3
<strong>Output:</strong> 8
<strong>Explanation:</strong>
After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right.
After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right.
After 3 seconds, the positions are [-3,-1,1].
The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2.
The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4.
The distance between the robot at index 0 and 1 is abs(-1 - 1) = 2.
The sum of the pairs of all distances = 2 + 4 + 2 = 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,0], s = &quot;RL&quot;, d = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong>
After 1 second, the positions are [2,-1].
After 2 seconds, the positions are [3,-2].
The distance between the two robots is abs(-2 - 3) = 5.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-2 * 10<sup>9</sup>&nbsp;&lt;= nums[i] &lt;= 2 * 10<sup>9</sup></code></li>
<li><code>0 &lt;= d &lt;= 10<sup>9</sup></code></li>
<li><code>nums.length == s.length&nbsp;</code></li>
<li><code>s</code> consists of &#39;L&#39; and &#39;R&#39; only</li>
<li><code>nums[i]</code>&nbsp;will be unique.</li>
</ul>

View File

@ -0,0 +1,35 @@
<p>You are given two numeric strings <code>num1</code> and <code>num2</code> and two integers <code>max_sum</code> and <code>min_sum</code>. We denote an integer <code>x</code> to be <em>good</em> if:</p>
<ul>
<li><code>num1 &lt;= x &lt;= num2</code></li>
<li><code>min_sum &lt;= digit_sum(x) &lt;= max_sum</code>.</li>
</ul>
<p>Return <em>the number of good integers</em>. Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
<p>Note that <code>digit_sum(x)</code> denotes the sum of the digits of <code>x</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num1 = &quot;1&quot;, num2 = &quot;12&quot;, <code>min_sum</code> = 1, max_sum = 8
<strong>Output:</strong> 11
<strong>Explanation:</strong> There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num1 = &quot;1&quot;, num2 = &quot;5&quot;, <code>min_sum</code> = 1, max_sum = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong> The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num1 &lt;= num2 &lt;= 10<sup>22</sup></code></li>
<li><code>1 &lt;= min_sum &lt;= max_sum &lt;= 400</code></li>
</ul>

View File

@ -0,0 +1,80 @@
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>. 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 <code>cancelFn</code> is called.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 20, cancelT = 110
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 8},
{&quot;time&quot;: 20, &quot;returned&quot;: 8},
{&quot;time&quot;: 40, &quot;returned&quot;: 8},
{&quot;time&quot;: 60, &quot;returned&quot;: 8},
{&quot;time&quot;: 80, &quot;returned&quot;: 8},
{&quot;time&quot;: 100, &quot;returned&quot;: 8}
]
<strong>Explanation:</strong> Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.
const cancel = cancellable(x =&gt; x * 2, [4], 20);
setTimeout(cancel, 110);
1st fn call is at 0ms. fn(4) returns 8.
2nd fn call is at 20ms. fn(4) returns 8.
3rd fn call is at 40ms. fn(4) returns 8.
4th fn call is at&nbsp;60ms. fn(4) returns 8.
5th fn call is at 80ms. fn(4) returns 8.
6th fn call is at 100ms. fn(4) returns 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 25, cancelT = 140
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 10},
{&quot;time&quot;: 25, &quot;returned&quot;: 10},
{&quot;time&quot;: 50, &quot;returned&quot;: 10},
{&quot;time&quot;: 75, &quot;returned&quot;: 10},
{&quot;time&quot;: 100, &quot;returned&quot;: 10},
{&quot;time&quot;: 125, &quot;returned&quot;: 10}
]
<strong>Explanation:</strong> Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.
1st fn call is at 0ms&nbsp;
2nd fn call is at 25ms&nbsp;
3rd fn call is at 50ms&nbsp;
4th fn call is at&nbsp;75ms&nbsp;
5th fn call is at 100ms&nbsp;
6th fn call is at 125ms
Cancelled at 140ms
</pre>
<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>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.
1st fn call is at 0ms
2nd fn call is at 50ms
3rd fn call is at 100ms
4th fn call is at&nbsp;150ms
Cancelled at 180ms
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>fn is a function</code></li>
<li><code>args is a valid JSON array</code></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>
</ul>

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,55 @@
{
"data": {
"question": {
"questionId": "2874",
"questionFrontendId": "2720",
"boundTopicId": null,
"title": "Popularity Percentage",
"titleSlug": "popularity-percentage",
"content": null,
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": true,
"difficulty": "Hard",
"likes": 6,
"dislikes": 2,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": null,
"categoryTitle": "Database",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": null,
"stats": "{\"totalAccepted\": \"87\", \"totalSubmission\": \"319\", \"totalAcceptedRaw\": 87, \"totalSubmissionRaw\": 319, \"acRate\": \"27.3%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "{\"headers\":{\"Friends\":[\"user1\",\"user2\"]},\"rows\":{\"Friends\":[[2,1],[1,3],[4,1],[1,5],[1,6],[2,6],[7,2],[8,3],[3,9]]}}",
"metaData": "{\n \"mysql\": [\n \"Create table if not exists Friends (user1 int, user2 int)\"\n ],\n \"mssql\": [\n \"Create table Friends(user1 int, user2 int)\"\n ],\n \"oraclesql\": [\n \"Create table Friends(user1 int, user2 int)\"\n ],\n \"database\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [
"Create table if not exists Friends (user1 int, user2 int)",
"Truncate table Friends",
"insert into Friends (user1, user2) values ('2', '1')",
"insert into Friends (user1, user2) values ('1', '3')",
"insert into Friends (user1, user2) values ('4', '1')",
"insert into Friends (user1, user2) values ('1', '5')",
"insert into Friends (user1, user2) values ('1', '6')",
"insert into Friends (user1, user2) values ('2', '6')",
"insert into Friends (user1, user2) values ('7', '2')",
"insert into Friends (user1, user2) values ('8', '3')",
"insert into Friends (user1, user2) values ('3', '9')"
],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2859",
"questionFrontendId": "2723",
"boundTopicId": null,
"title": "Add Two Promises",
"titleSlug": "add-two-promises",
"content": "Given two promises <code>promise1</code> and <code>promise2</code>, return a new promise. <code>promise1</code> and <code>promise2</code>&nbsp;will both resolve with a number. The returned promise should resolve with the sum of the two numbers.\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \npromise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(2), 20)), \npromise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 60))\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \npromise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 50)), \npromise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(-12), 30))\n<strong>Output:</strong> -2\n<strong>Explanation:</strong> The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>promise1 and promise2 are&nbsp;promises that resolve&nbsp;with a number</code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 23,
"dislikes": 2,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "new Promise(resolve => setTimeout(() => resolve(2), 20))\nnew Promise(resolve => setTimeout(() => resolve(5), 60))\nnew Promise(resolve => setTimeout(() => resolve(10), 50))\nnew Promise(resolve => setTimeout(() => resolve(-12), 30))",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Promise} promise1\n * @param {Promise} promise2\n * @return {Promise}\n */\nvar addTwoPromises = async function(promise1, promise2) {\n \n};\n\n/**\n * addTwoPromises(Promise.resolve(2), Promise.resolve(2))\n * .then(console.log); // 4\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "async function addTwoPromises(promise1: Promise<number>, promise2: Promise<number>): Promise<number> {\n\n};\n\n/**\n * addTwoPromises(Promise.resolve(2), Promise.resolve(2))\n * .then(console.log); // 4\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1205, \"totalSubmissionRaw\": 1331, \"acRate\": \"90.5%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "new Promise(resolve => setTimeout(() => resolve(2), 20))\nnew Promise(resolve => setTimeout(() => resolve(5), 60))",
"metaData": "{\n \"name\": \"addTwoPromises\",\n \"params\": [\n {\n \"name\": \"promise1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"promise2\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2863",
"questionFrontendId": "2726",
"boundTopicId": null,
"title": "Calculator with Method Chaining",
"titleSlug": "calculator-with-method-chaining",
"content": "<p>Design a <code>Calculator</code> class. The class should provide the&nbsp;mathematical operations of&nbsp;addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining.&nbsp;The <code>Calculator</code> class constructor should accept a number&nbsp;which serves as the&nbsp;initial value of <code>result</code>.</p>\n\n<p>Your <font face=\"monospace\"><code>Calculator</code>&nbsp;</font>class should have the following methods:</p>\n\n<ul>\n\t<li><code>add</code> - This method adds the given number <code>value</code> to the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>subtract</code> -&nbsp;This method subtracts the given number <code>value</code>&nbsp;from the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>multiply</code> -&nbsp;This method multiplies the <code>result</code>&nbsp; by the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>divide</code> -&nbsp;This method divides the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>. If the passed value is <code>0</code>, an error <code>&quot;Division by zero is not allowed&quot;</code> should be thrown.</li>\n\t<li><code>power</code> -&nbsp;This method raises the&nbsp;<code>result</code> to the power of the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>getResult</code> -&nbsp;This method returns the <code>result</code>.</li>\n</ul>\n\n<p>Solutions within&nbsp;<code>10<sup>-5</sup></code>&nbsp;of the actual result are considered correct.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;getResult&quot;], values = [10, 5, 7]\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> \nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;multiply&quot;, &quot;power&quot;, &quot;getResult&quot;], values = [2, 5, 2]\n<strong>Output:</strong> 100\n<strong>Explanation:</strong> \nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;divide&quot;, &quot;getResult&quot;], values = [20, 0]\n<strong>Output:</strong> &quot;Division by zero is not allowed&quot;\n<strong>Explanation:</strong> \nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\nThe error should be thrown because we cannot divide by zero.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup></code><code>&nbsp;- 1</code></li>\n\t<li><code>actions[i] is one of &quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;multiply&quot;, &quot;divide&quot;, &quot;power&quot;, and&nbsp;&quot;getResult&quot;</code></li>\n\t<li><code><font face=\"monospace\">Last action is always &quot;getResult&quot;</font></code></li>\n\t<li><code><font face=\"monospace\">values is a JSON array of numbers</font></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 13,
"dislikes": 2,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]\n[\"Calculator\", \"multiply\", \"power\", \"getResult\"]\n[2, 5, 2]\n[\"Calculator\", \"divide\", \"getResult\"]\n[20, 0]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "class Calculator {\n \n /** \n * @param {number} value\n */\n constructor(value) {\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n add(value){\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n subtract(value){\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */ \n multiply(value) {\n\n }\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n divide(value) {\n\n }\n \n /** \n * @param {number} value\n * @return {Calculator}\n */\n power(value) {\n\n }\n \n /** \n * @return {number}\n */\n getResult() {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "class Calculator {\n \n constructor(value : number) {\n\n }\n \n add(value : number) : Calculator {\n\n }\n \n subtract(value : number) : Calculator {\n\n }\n \n multiply(value : number) : Calculator {\n\n }\n\n divide(value : number) : Calculator {\n\n }\n \n power(value : number) : Calculator {\n\n }\n\n getResult() : number {\n \n }\n}",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"721\", \"totalSubmission\": \"920\", \"totalAcceptedRaw\": 721, \"totalSubmissionRaw\": 920, \"acRate\": \"78.4%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]",
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"actions\",\n \"type\": \"string[]\"\n },\n {\n \"type\": \"double[]\",\n \"name\": \"values\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2807",
"questionFrontendId": "2721",
"boundTopicId": null,
"title": "Execute Asynchronous Functions in Parallel",
"titleSlug": "execute-asynchronous-functions-in-parallel",
"content": "<p>Given an array of asynchronous&nbsp;functions&nbsp;<code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments&nbsp;and returns a promise.</p>\n\n<p><code>promise</code> resolves:</p>\n\n<ul>\n\t<li>When all the promises returned from&nbsp;<code>functions</code>&nbsp;were resolved successfully.&nbsp;The resolved&nbsp;value of&nbsp;<code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the&nbsp;<code>functions.</code></li>\n</ul>\n\n<p><code>promise</code> rejects:</p>\n\n<ul>\n\t<li>When any&nbsp;of the promises&nbsp;returned from&nbsp;<code>functions</code>&nbsp;were rejected.&nbsp;<code>promise</code> should also&nbsp;reject&nbsp;with the reason of the first rejection.</li>\n</ul>\n\n<p>Please solve it without using the built-in&nbsp;<code>Promise.all</code>&nbsp;function.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))\n]\n<strong>Output:</strong> {&quot;t&quot;: 200, &quot;resolved&quot;: [5]}\n<strong>Explanation:</strong> \npromiseAll(functions).then(console.log); // [5]\n\nThe single function was resolved at 200ms with a value of 5.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)), \n () =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(&quot;Error&quot;), 100))\n]\n<strong>Output:</strong> {&quot;t&quot;: 100, &quot;rejected&quot;: &quot;Error&quot;}\n<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [\n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)), \n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)), \n () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))\n]\n<strong>Output:</strong> {&quot;t&quot;: 150, &quot;resolved&quot;: [4, 10, 16]}\n<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>functions&nbsp;is an array of functions that returns promises</code></li>\n\t<li><code>1 &lt;= functions.length &lt;= 10</code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 14,
"dislikes": 3,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "[() => new Promise(resolve => setTimeout(() => resolve(5), 200))]\n[() => new Promise(resolve => setTimeout(() => resolve(1), 200)), () => new Promise((resolve, reject) => setTimeout(() => reject(\"Error\"), 100))]\n[() => new Promise(resolve => setTimeout(() => resolve(4), 50)), () => new Promise(resolve => setTimeout(() => resolve(10), 150)), () => new Promise(resolve => setTimeout(() => resolve(16), 100))]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array<Function>} functions\n * @return {Promise<any>}\n */\nvar promiseAll = async function(functions) {\n \n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "async function promiseAll<T>(functions: (() => Promise<T>)[]): Promise<T[]> {\n\n};\n\n/**\n * const promise = promiseAll([() => new Promise(res => res(42))])\n * promise.then(console.log); // [42]\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"455\", \"totalSubmission\": \"529\", \"totalAcceptedRaw\": 455, \"totalSubmissionRaw\": 529, \"acRate\": \"86.0%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[() => new Promise(resolve => setTimeout(() => resolve(5), 200))]",
"metaData": "{\n \"name\": \"promiseAll\",\n \"params\": [\n {\n \"name\": \"functions\",\n \"type\": \"string[]\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2821",
"questionFrontendId": "2715",
"boundTopicId": null,
"title": "Execute Cancellable Function With Delay",
"titleSlug": "execute-cancellable-function-with-delay",
"content": "<p>Given a function <code>fn</code>, an array or arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>\n\n<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case,&nbsp;<code>fn</code> should never be called.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50\n<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]\n<strong>Explanation:</strong> \nconst cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms\nsetTimeout(cancel, 50);\n\nthe cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50\n<strong>Output:</strong> []\n<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100\n<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]\n<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime &gt; t.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn is a function</code></li>\n\t<li><code>args is a valid JSON array</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 27,
"dislikes": 6,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "(x) => x * 5\n[2]\n20\n50\n(x) => x**2\n[2]\n100\n50\n(x1, x2) => x1 * x2\n[2,4]\n20\n100",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1232, \"totalSubmissionRaw\": 1348, \"acRate\": \"91.4%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "(x) => x * 5\n[2]\n20\n50",
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2862",
"questionFrontendId": "2725",
"boundTopicId": null,
"title": "Interval Cancellation",
"titleSlug": "interval-cancellation",
"content": "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>. 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 <code>cancelFn</code> is called.\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 20, cancelT = 110\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 8},\n {&quot;time&quot;: 20, &quot;returned&quot;: 8},\n {&quot;time&quot;: 40, &quot;returned&quot;: 8},\n {&quot;time&quot;: 60, &quot;returned&quot;: 8},\n {&quot;time&quot;: 80, &quot;returned&quot;: 8},\n {&quot;time&quot;: 100, &quot;returned&quot;: 8}\n]\n<strong>Explanation:</strong> Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.\n\nconst cancel = cancellable(x =&gt; x * 2, [4], 20);\nsetTimeout(cancel, 110);\n\n1st fn call is at 0ms. fn(4) returns 8.\n2nd fn call is at 20ms. fn(4) returns 8.\n3rd fn call is at 40ms. fn(4) returns 8.\n4th fn call is at&nbsp;60ms. fn(4) returns 8.\n5th fn call is at 80ms. fn(4) returns 8.\n6th fn call is at 100ms. fn(4) returns 8.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 25, cancelT = 140\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 10},\n {&quot;time&quot;: 25, &quot;returned&quot;: 10},\n {&quot;time&quot;: 50, &quot;returned&quot;: 10},\n {&quot;time&quot;: 75, &quot;returned&quot;: 10},\n {&quot;time&quot;: 100, &quot;returned&quot;: 10},\n {&quot;time&quot;: 125, &quot;returned&quot;: 10}\n]\n<strong>Explanation:</strong> Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.\n1st fn call is at 0ms&nbsp;\n2nd fn call is at 25ms&nbsp;\n3rd fn call is at 50ms&nbsp;\n4th fn call is at&nbsp;75ms&nbsp;\n5th fn call is at 100ms&nbsp;\n6th fn call is at 125ms\nCancelled at 140ms\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2, x3) =&gt; (x1 + x2 + x3), args = [5, 1, 3], t = 50, cancelT = 180\n<strong>Output:</strong> \n[\n {&quot;time&quot;: 0, &quot;returned&quot;: 9},\n {&quot;time&quot;: 50, &quot;returned&quot;: 9},\n {&quot;time&quot;: 100, &quot;returned&quot;: 9},\n {&quot;time&quot;: 150, &quot;returned&quot;: 9}\n]\n<strong>Explanation:</strong> Every 50ms, fn(5, 1, 3) is called. Until t=180ms, then it is cancelled. \n1st fn call is at 0ms\n2nd fn call is at 50ms\n3rd fn call is at 100ms\n4th fn call is at&nbsp;150ms\nCancelled at 180ms\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn is a function</code></li>\n\t<li><code>args is a valid JSON array</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 21,
"dislikes": 4,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "(x) => x * 2\n[4]\n20\n110\n(x1, x2) => (x1 * x2)\n[2,5]\n25\n140\n(x1, x2, x3) => (x1 + x2 + x3)\n[5,1,3]\n50\n160",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 2\n * const args = [4], t = 20, cancelT = 110\n *\n * const start = performance.now()\n *\n * const log = (...argsArr) => {\n *\t\tconst val = fn(...argsArr)\n * result.push({\"time\": Math.floor(performance.now() - start), \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":20,\"returned\":8},\n * // {\"time\":40,\"returned\":8}, \n * // {\"time\":60,\"returned\":8},\n * // {\"time\":80,\"returned\":8},\n * // {\"time\":100,\"returned\":8}\n * // ]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 2\n * const args = [4], t = 20, cancelT = 110\n *\n * const start = performance.now()\n *\n * const log = (...argsArr) => {\n *\t\tconst val = fn(...argsArr)\n * result.push({\"time\": Math.floor(performance.now() - start), \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [\n * // {\"time\":0,\"returned\":8},\n * // {\"time\":20,\"returned\":8},\n * // {\"time\":40,\"returned\":8}, \n * // {\"time\":60,\"returned\":8},\n * // {\"time\":80,\"returned\":8},\n * // {\"time\":100,\"returned\":8}\n * // ]\n * }, cancelT)\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"726\", \"totalSubmission\": \"783\", \"totalAcceptedRaw\": 726, \"totalSubmissionRaw\": 783, \"acRate\": \"92.7%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "(x) => x * 2\n[4]\n20\n110",
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2864",
"questionFrontendId": "2727",
"boundTopicId": null,
"title": "Is Object Empty",
"titleSlug": "is-object-empty",
"content": "<p>Given an object or an array, return if it is empty.</p>\n\n<ul>\n\t<li>An empty object contains no key-value pairs.</li>\n\t<li>An empty array contains no elements.</li>\n</ul>\n\n<p>You may assume the object or array is the output of&nbsp;<code>JSON.parse</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = {&quot;x&quot;: 5, &quot;y&quot;: 42}\n<strong>Output:</strong> false\n<strong>Explanation:</strong> The object has 2 key-value pairs so it is not empty.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = {}\n<strong>Output:</strong> true\n<strong>Explanation:</strong> The object doesn&#39;t have any key-value pairs so it is empty.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = [null, false, 0]\n<strong>Output:</strong> false\n<strong>Explanation:</strong> The array has 3 elements so it is not empty.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>&nbsp;<code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>\n</ul>\n\n<p>&nbsp;</p>\n<strong>Can you solve it in O(1) time?</strong>",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 25,
"dislikes": 3,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "{\"x\": 5, \"y\": 42}\n{}\n[null, false, 0]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Object | Array} obj\n * @return {boolean}\n */\nvar isEmpty = function(obj) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function isEmpty(obj: Record<string, any> | any[]): boolean {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"1.3K\", \"totalSubmission\": \"1.6K\", \"totalAcceptedRaw\": 1321, \"totalSubmissionRaw\": 1598, \"acRate\": \"82.7%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "{\"x\": 5, \"y\": 42}",
"metaData": "{\n \"name\": \"isEmpty\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2858",
"questionFrontendId": "2722",
"boundTopicId": null,
"title": "Join Two Arrays by ID",
"titleSlug": "join-two-arrays-by-id",
"content": "<p>Given two arrays <code>arr1</code> and <code>arr2</code>, return a new&nbsp;array <code>joinedArray</code>. All the objects in each&nbsp;of the two inputs arrays will contain an&nbsp;<code>id</code>&nbsp;field that has an integer value.&nbsp;<code>joinedArray</code>&nbsp;is an array formed by merging&nbsp;<code>arr1</code> and <code>arr2</code> based on&nbsp;their <code>id</code>&nbsp;key. The length of&nbsp;<code>joinedArray</code> should be the length of unique values of <code>id</code>. The returned array should be sorted in&nbsp;<strong>ascending</strong>&nbsp;order based on the <code>id</code>&nbsp;key.</p>\n\n<p>If a given&nbsp;<code>id</code>&nbsp;exists in one array but not the other, the single object with that&nbsp;<code>id</code> should be included in the result array without modification.</p>\n\n<p>If two objects share an <code>id</code>, their properties should be merged into a single&nbsp;object:</p>\n\n<ul>\n\t<li>If a key only exists in one object, that single key-value pair should be included in the object.</li>\n\t<li>If a key is included in both objects, the value in the object from <code>arr2</code>&nbsp;should override the value from <code>arr1</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},\n&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9}\n], \narr2 = [\n {&quot;id&quot;: 3, &quot;x&quot;: 5}\n]\n<strong>Output:</strong> \n[\n&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},\n&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9},\n {&quot;id&quot;: 3, &quot;x&quot;: 5}\n]\n<strong>Explanation:</strong> There are no duplicate ids so arr1 is simply concatenated with arr2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n {&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},\n {&quot;id&quot;: 2, &quot;x&quot;: 3, &quot;y&quot;: 6}\n], \narr2 = [\n {&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},\n {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}\n]\n<strong>Output:</strong> \n[\n {&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},\n {&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},\n&nbsp; {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}\n]\n<strong>Explanation:</strong> The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr1 = [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;b&quot;: 94},&quot;v&quot;: [4, 3], &quot;y&quot;: 48}\n]\narr2 = [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3]}\n]\n<strong>Output:</strong> [\n {&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3], &quot;y&quot;: 48}\n]\n<strong>Explanation:</strong> The two objects with id=1 are merged together. For the keys &quot;b&quot; and &quot;v&quot; the values from arr2 are used. Since the key &quot;y&quot; only exists in arr1, that value is taken form arr1.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr1 and arr2 are valid JSON arrays</code></li>\n\t<li><code>Each object in arr1 and arr2 has a unique&nbsp;integer id key</code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>\n\t<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 13,
"dislikes": 3,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]\n[{\"id\": 1,\"x\": 2,\"y\": 3},{\"id\": 2,\"x\": 3,\"y\": 6}]\n[{\"id\": 2,\"x\": 10,\"y\": 20},{\"id\": 3,\"x\": 0,\"y\": 0}]\n[{\"id\":1,\"b\":{\"b\": 94},\"v\":[4,3],\"y\":48}]\n[{\"id\":1,\"b\":{\"c\": 84},\"v\":[1,3]}]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array} arr1\n * @param {Array} arr2\n * @return {Array}\n */\nvar join = function(arr1, arr2) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function join(arr1: any[], arr2: any[]): any[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"571\", \"totalSubmission\": \"880\", \"totalAcceptedRaw\": 571, \"totalSubmissionRaw\": 880, \"acRate\": \"64.9%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[{\"id\": 1,\"x\": 1},{\"id\": 2,\"x\": 9}]\n[{\"id\": 3,\"x\": 5}]",
"metaData": "{\n \"name\": \"join\",\n \"params\": [\n {\n \"name\": \"arr1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"arr2\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,56 @@
{
"data": {
"question": {
"questionId": "2860",
"questionFrontendId": "2724",
"boundTopicId": null,
"title": "Sort By",
"titleSlug": "sort-by",
"content": "<p>Given an array <code>arr</code> and a function <code>fn</code>, return a sorted array <code>sortedArr</code>. You can assume&nbsp;<code>fn</code>&nbsp;only returns numbers and those numbers determine the sort order of&nbsp;<code>sortedArr</code>. <code>sortedArray</code> must be sorted in <strong>ascending order</strong> by <code>fn</code> output.</p>\n\n<p>You may assume that <code>fn</code> will never duplicate numbers for a given array.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [5, 4, 1, 2, 3], fn = (x) =&gt; x\n<strong>Output:</strong> [1, 2, 3, 4, 5]\n<strong>Explanation:</strong> fn simply returns the number passed to it so the array is sorted in ascending order.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [{&quot;x&quot;: 1}, {&quot;x&quot;: 0}, {&quot;x&quot;: -1}], fn = (d) =&gt; d.x\n<strong>Output:</strong> [{&quot;x&quot;: -1}, {&quot;x&quot;: 0}, {&quot;x&quot;: 1}]\n<strong>Explanation:</strong> fn returns the value for the &quot;x&quot; key. So the array is sorted based on that value.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [[3, 4], [5, 2], [10, 1]], fn = (x) =&gt; x[1]\n<strong>Output:</strong> [[10, 1], [5, 2], [3, 4]]\n<strong>Explanation:</strong> arr is sorted in ascending order by number at index=1.&nbsp;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr is a valid JSON array</code></li>\n\t<li><code>fn is a function that returns a number</code></li>\n\t<li><code>1 &lt;=&nbsp;arr.length &lt;= 5 * 10<sup>5</sup></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 17,
"dislikes": 3,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "[5,4,1,2,3]\n(x) => x\n[{\"x\":1},{\"x\": 0},{\"x\": -1}]\n(x) => x.x\n[[3,4],[5,2],[10,1]]\n(x) => x[1]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */\nvar sortBy = function(arr, fn) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function sortBy(arr: any[], fn: Function): any[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"868\", \"totalSubmission\": \"1K\", \"totalAcceptedRaw\": 868, \"totalSubmissionRaw\": 1043, \"acRate\": \"83.2%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[5,4,1,2,3]\n(x) => x",
"metaData": "{\n \"name\": \"sortBy\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,28 @@
Given two promises <code>promise1</code> and <code>promise2</code>, return a new promise. <code>promise1</code> and <code>promise2</code>&nbsp;will both resolve with a number. The returned promise should resolve with the sum of the two numbers.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong>
promise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(2), 20)),
promise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 60))
<strong>Output:</strong> 7
<strong>Explanation:</strong> The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong>
promise1 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 50)),
promise2 = new Promise(resolve =&gt; setTimeout(() =&gt; resolve(-12), 30))
<strong>Output:</strong> -2
<strong>Explanation:</strong> The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>promise1 and promise2 are&nbsp;promises that resolve&nbsp;with a number</code></li>
</ul>

View File

@ -0,0 +1,55 @@
<p>Design a <code>Calculator</code> class. The class should provide the&nbsp;mathematical operations of&nbsp;addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining.&nbsp;The <code>Calculator</code> class constructor should accept a number&nbsp;which serves as the&nbsp;initial value of <code>result</code>.</p>
<p>Your <font face="monospace"><code>Calculator</code>&nbsp;</font>class should have the following methods:</p>
<ul>
<li><code>add</code> - This method adds the given number <code>value</code> to the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>
<li><code>subtract</code> -&nbsp;This method subtracts the given number <code>value</code>&nbsp;from the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>
<li><code>multiply</code> -&nbsp;This method multiplies the <code>result</code>&nbsp; by the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>
<li><code>divide</code> -&nbsp;This method divides the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>. If the passed value is <code>0</code>, an error <code>&quot;Division by zero is not allowed&quot;</code> should be thrown.</li>
<li><code>power</code> -&nbsp;This method raises the&nbsp;<code>result</code> to the power of the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>
<li><code>getResult</code> -&nbsp;This method returns the <code>result</code>.</li>
</ul>
<p>Solutions within&nbsp;<code>10<sup>-5</sup></code>&nbsp;of the actual result are considered correct.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;getResult&quot;], values = [10, 5, 7]
<strong>Output:</strong> 8
<strong>Explanation:</strong>
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;multiply&quot;, &quot;power&quot;, &quot;getResult&quot;], values = [2, 5, 2]
<strong>Output:</strong> 100
<strong>Explanation:</strong>
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> actions = [&quot;Calculator&quot;, &quot;divide&quot;, &quot;getResult&quot;], values = [20, 0]
<strong>Output:</strong> &quot;Division by zero is not allowed&quot;
<strong>Explanation:</strong>
new Calculator(20).divide(0).getResult() // 20 / 0
The error should be thrown because we cannot divide by zero.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup></code><code>&nbsp;- 1</code></li>
<li><code>actions[i] is one of &quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;multiply&quot;, &quot;divide&quot;, &quot;power&quot;, and&nbsp;&quot;getResult&quot;</code></li>
<li><code><font face="monospace">Last action is always &quot;getResult&quot;</font></code></li>
<li><code><font face="monospace">values is a JSON array of numbers</font></code></li>
</ul>

View File

@ -0,0 +1,35 @@
<p>You are given an integer <code>n</code> that consists of exactly <code>3</code> digits.</p>
<p>We call the number <code>n</code> <strong>fascinating</strong> if, after the following modification, the resulting number contains all the digits from <code>1</code> to <code>9</code> <strong>exactly</strong> once and does not contain any <code>0</code>&#39;s:</p>
<ul>
<li><strong>Concatenate</strong> <code>n</code> with the numbers <code>2 * n</code> and <code>3 * n</code>.</li>
</ul>
<p>Return <code>true</code><em> if </em><code>n</code><em> is fascinating, or </em><code>false</code><em> otherwise</em>.</p>
<p><strong>Concatenating</strong> two numbers means joining them together. For example, the concatenation of <code>121</code> and <code>371</code> is <code>121371</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 192
<strong>Output:</strong> true
<strong>Explanation:</strong> We concatenate the numbers n = 192 and 2 * n = 384 and 3 * n = 576. The resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 100
<strong>Output:</strong> false
<strong>Explanation:</strong> We concatenate the numbers n = 100 and 2 * n = 200 and 3 * n = 300. The resulting number is 100200300. This number does not satisfy any of the conditions.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>100 &lt;= n &lt;= 999</code></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index <code>i</code>&nbsp;is <code>nums[i]</code>. Each chocolate is of a different type, and initially, the chocolate at the index&nbsp;<code>i</code>&nbsp;is of <code>i<sup>th</sup></code> type.</p>
<p>In one operation, you can do the following with an incurred <strong>cost</strong> of <code>x</code>:</p>
<ul>
<li>Simultaneously change the chocolate of <code>i<sup>th</sup></code> type to <code>((i + 1) mod n)<sup>th</sup></code> type for all chocolates.</li>
</ul>
<p>Return <em>the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [20,1,15], x = 5
<strong>Output:</strong> 13
<strong>Explanation:</strong> Initially, the chocolate types are [0,1,2]. We will buy the 1<sup>st</sup>&nbsp;type of chocolate at a cost of 1.
Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2<sup>nd</sup><sup> </sup>type of chocolate at a cost of 1.
Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0<sup>th </sup>type of chocolate at a cost of 1.
Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3], x = 4
<strong>Output:</strong> 6
<strong>Explanation:</strong> We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,35 @@
<p>You are given two numeric strings <code>num1</code> and <code>num2</code> and two integers <code>max_sum</code> and <code>min_sum</code>. We denote an integer <code>x</code> to be <em>good</em> if:</p>
<ul>
<li><code>num1 &lt;= x &lt;= num2</code></li>
<li><code>min_sum &lt;= digit_sum(x) &lt;= max_sum</code>.</li>
</ul>
<p>Return <em>the number of good integers</em>. Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
<p>Note that <code>digit_sum(x)</code> denotes the sum of the digits of <code>x</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num1 = &quot;1&quot;, num2 = &quot;12&quot;, <code>min_sum</code> = 1, max_sum = 8
<strong>Output:</strong> 11
<strong>Explanation:</strong> There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num1 = &quot;1&quot;, num2 = &quot;5&quot;, <code>min_sum</code> = 1, max_sum = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong> The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num1 &lt;= num2 &lt;= 10<sup>22</sup></code></li>
<li><code>1 &lt;= min_sum &lt;= max_sum &lt;= 400</code></li>
</ul>

View File

@ -0,0 +1,60 @@
<p>Given an array of asynchronous&nbsp;functions&nbsp;<code>functions</code>, return a new promise <code>promise</code>. Each function in the array accepts no arguments&nbsp;and returns a promise.</p>
<p><code>promise</code> resolves:</p>
<ul>
<li>When all the promises returned from&nbsp;<code>functions</code>&nbsp;were resolved successfully.&nbsp;The resolved&nbsp;value of&nbsp;<code>promise</code> should be an array of all the resolved values of promises in the same order as they were in the&nbsp;<code>functions.</code></li>
</ul>
<p><code>promise</code> rejects:</p>
<ul>
<li>When any&nbsp;of the promises&nbsp;returned from&nbsp;<code>functions</code>&nbsp;were rejected.&nbsp;<code>promise</code> should also&nbsp;reject&nbsp;with the reason of the first rejection.</li>
</ul>
<p>Please solve it without using the built-in&nbsp;<code>Promise.all</code>&nbsp;function.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> functions = [
&nbsp; () =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(5), 200))
]
<strong>Output:</strong> {&quot;t&quot;: 200, &quot;resolved&quot;: [5]}
<strong>Explanation:</strong>
promiseAll(functions).then(console.log); // [5]
The single function was resolved at 200ms with a value of 5.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> functions = [
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(1), 200)),
() =&gt; new Promise((resolve, reject) =&gt; setTimeout(() =&gt; reject(&quot;Error&quot;), 100))
]
<strong>Output:</strong> {&quot;t&quot;: 100, &quot;rejected&quot;: &quot;Error&quot;}
<strong>Explanation:</strong> Since one of the promises rejected, the returned promise also rejected with the same error at the same time.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> functions = [
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(4), 50)),
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(10), 150)),
() =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(16), 100))
]
<strong>Output:</strong> {&quot;t&quot;: 150, &quot;resolved&quot;: [4, 10, 16]}
<strong>Explanation:</strong> All the promises resolved with a value. The returned promise resolved when the last promise resolved.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>functions&nbsp;is an array of functions that returns promises</code></li>
<li><code>1 &lt;= functions.length &lt;= 10</code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>Given a function <code>fn</code>, an array or 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&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case,&nbsp;<code>fn</code> should never be called.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelTime = 50
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
<strong>Explanation:</strong>
const cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms
setTimeout(cancel, 50);
the cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelTime = 50
<strong>Output:</strong> []
<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime &gt; t.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>fn is a function</code></li>
<li><code>args is a valid JSON array</code></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>
</ul>

View File

@ -0,0 +1,54 @@
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>grid</code>.</p>
<p>Let us call a <strong>non-empty</strong> subset of rows <strong>good</strong> if the sum of each column of the subset is at most half of the length of the subset.</p>
<p>More formally, if the length of the chosen subset of rows is <code>k</code>, then the sum of each column should be at most <code>floor(k / 2)</code>.</p>
<p>Return <em>an integer array that contains row indices of a good subset sorted in <strong>ascending</strong> order.</em></p>
<p>If there are multiple good subsets, you can return any of them. If there are no good subsets, return an empty array.</p>
<p>A <strong>subset</strong> of rows of the matrix <code>grid</code> is any matrix that can be obtained by deleting some (possibly none or all) rows from <code>grid</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,0,0,1],[1,1,1,1]]
<strong>Output:</strong> [0,1]
<strong>Explanation:</strong> We can choose the 0<sup>th</sup> and 1<sup>st</sup> rows to create a good subset of rows.
The length of the chosen subset is 2.
- The sum of the 0<sup>th</sup>&nbsp;column is 0 + 0 = 0, which is at most half of the length of the subset.
- The sum of the 1<sup>st</sup>&nbsp;column is 1 + 0 = 1, which is at most half of the length of the subset.
- The sum of the 2<sup>nd</sup>&nbsp;column is 1 + 0 = 1, which is at most half of the length of the subset.
- The sum of the 3<sup>rd</sup>&nbsp;column is 0 + 1 = 1, which is at most half of the length of the subset.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> grid = [[0]]
<strong>Output:</strong> [0]
<strong>Explanation:</strong> We can choose the 0<sup>th</sup> row to create a good subset of rows.
The length of the chosen subset is 1.
- The sum of the 0<sup>th</sup>&nbsp;column is 0, which is at most half of the length of the subset.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> grid = [[1,1,1],[1,1,1]]
<strong>Output:</strong> []
<strong>Explanation:</strong> It is impossible to choose any subset of rows to create a good subset.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= n &lt;= 5</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

View File

@ -0,0 +1,40 @@
<p>You are given a <strong>0-indexed</strong> string <code>s</code> that consists of digits from <code>0</code> to <code>9</code>.</p>
<p>A string <code>t</code> is called a <strong>semi-repetitive</strong> if there is at most one consecutive pair of the same digits inside <code>t</code>. For example, <code>0010</code>, <code>002020</code>, <code>0123</code>, <code>2002</code>, and <code>54944</code> are semi-repetitive while&nbsp;<code>00101022</code>, and <code>1101234883</code> are not.</p>
<p>Return <em>the length of the longest semi-repetitive substring inside</em> <code>s</code>.</p>
<p>A <b>substring</b> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;52233&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest semi-repetitive substring is &quot;5223&quot;, which starts at i = 0 and ends at j = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;5494&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> s is a semi-reptitive string, so the answer is 4.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;1111111&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest semi-repetitive substring is &quot;11&quot;, which starts at i = 0 and ends at j = 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>&#39;0&#39; &lt;= s[i] &lt;= &#39;9&#39;</code></li>
</ul>

View File

@ -0,0 +1,80 @@
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>. 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 <code>cancelFn</code> is called.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x * 2, args = [4], t = 20, cancelT = 110
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 8},
{&quot;time&quot;: 20, &quot;returned&quot;: 8},
{&quot;time&quot;: 40, &quot;returned&quot;: 8},
{&quot;time&quot;: 60, &quot;returned&quot;: 8},
{&quot;time&quot;: 80, &quot;returned&quot;: 8},
{&quot;time&quot;: 100, &quot;returned&quot;: 8}
]
<strong>Explanation:</strong> Every 20ms, fn(4) is called. Until t=110ms, then it is cancelled.
const cancel = cancellable(x =&gt; x * 2, [4], 20);
setTimeout(cancel, 110);
1st fn call is at 0ms. fn(4) returns 8.
2nd fn call is at 20ms. fn(4) returns 8.
3rd fn call is at 40ms. fn(4) returns 8.
4th fn call is at&nbsp;60ms. fn(4) returns 8.
5th fn call is at 80ms. fn(4) returns 8.
6th fn call is at 100ms. fn(4) returns 8.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> fn = (x1, x2) =&gt; (x1 * x2), args = [2, 5], t = 25, cancelT = 140
<strong>Output:</strong>
[
{&quot;time&quot;: 0, &quot;returned&quot;: 10},
{&quot;time&quot;: 25, &quot;returned&quot;: 10},
{&quot;time&quot;: 50, &quot;returned&quot;: 10},
{&quot;time&quot;: 75, &quot;returned&quot;: 10},
{&quot;time&quot;: 100, &quot;returned&quot;: 10},
{&quot;time&quot;: 125, &quot;returned&quot;: 10}
]
<strong>Explanation:</strong> Every 25ms, fn(2, 5) is called. Until t=140ms, then it is cancelled.
1st fn call is at 0ms&nbsp;
2nd fn call is at 25ms&nbsp;
3rd fn call is at 50ms&nbsp;
4th fn call is at&nbsp;75ms&nbsp;
5th fn call is at 100ms&nbsp;
6th fn call is at 125ms
Cancelled at 140ms
</pre>
<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>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.
1st fn call is at 0ms
2nd fn call is at 50ms
3rd fn call is at 100ms
4th fn call is at&nbsp;150ms
Cancelled at 180ms
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>fn is a function</code></li>
<li><code>args is a valid JSON array</code></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>
</ul>

View File

@ -0,0 +1,43 @@
<p>Given an object or an array, return if it is empty.</p>
<ul>
<li>An empty object contains no key-value pairs.</li>
<li>An empty array contains no elements.</li>
</ul>
<p>You may assume the object or array is the output of&nbsp;<code>JSON.parse</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> obj = {&quot;x&quot;: 5, &quot;y&quot;: 42}
<strong>Output:</strong> false
<strong>Explanation:</strong> The object has 2 key-value pairs so it is not empty.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> obj = {}
<strong>Output:</strong> true
<strong>Explanation:</strong> The object doesn&#39;t have any key-value pairs so it is empty.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> obj = [null, false, 0]
<strong>Output:</strong> false
<strong>Explanation:</strong> The array has 3 elements so it is not empty.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>&nbsp;<code>2 &lt;= JSON.stringify(obj).length &lt;= 10<sup>5</sup></code></li>
</ul>
<p>&nbsp;</p>
<strong>Can you solve it in O(1) time?</strong>

View File

@ -0,0 +1,77 @@
<p>Given two arrays <code>arr1</code> and <code>arr2</code>, return a new&nbsp;array <code>joinedArray</code>. All the objects in each&nbsp;of the two inputs arrays will contain an&nbsp;<code>id</code>&nbsp;field that has an integer value.&nbsp;<code>joinedArray</code>&nbsp;is an array formed by merging&nbsp;<code>arr1</code> and <code>arr2</code> based on&nbsp;their <code>id</code>&nbsp;key. The length of&nbsp;<code>joinedArray</code> should be the length of unique values of <code>id</code>. The returned array should be sorted in&nbsp;<strong>ascending</strong>&nbsp;order based on the <code>id</code>&nbsp;key.</p>
<p>If a given&nbsp;<code>id</code>&nbsp;exists in one array but not the other, the single object with that&nbsp;<code>id</code> should be included in the result array without modification.</p>
<p>If two objects share an <code>id</code>, their properties should be merged into a single&nbsp;object:</p>
<ul>
<li>If a key only exists in one object, that single key-value pair should be included in the object.</li>
<li>If a key is included in both objects, the value in the object from <code>arr2</code>&nbsp;should override the value from <code>arr1</code>.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong>
arr1 = [
&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},
&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9}
],
arr2 = [
{&quot;id&quot;: 3, &quot;x&quot;: 5}
]
<strong>Output:</strong>
[
&nbsp; {&quot;id&quot;: 1, &quot;x&quot;: 1},
&nbsp; {&quot;id&quot;: 2, &quot;x&quot;: 9},
{&quot;id&quot;: 3, &quot;x&quot;: 5}
]
<strong>Explanation:</strong> There are no duplicate ids so arr1 is simply concatenated with arr2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong>
arr1 = [
{&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},
{&quot;id&quot;: 2, &quot;x&quot;: 3, &quot;y&quot;: 6}
],
arr2 = [
{&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},
{&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}
]
<strong>Output:</strong>
[
{&quot;id&quot;: 1, &quot;x&quot;: 2, &quot;y&quot;: 3},
{&quot;id&quot;: 2, &quot;x&quot;: 10, &quot;y&quot;: 20},
&nbsp; {&quot;id&quot;: 3, &quot;x&quot;: 0, &quot;y&quot;: 0}
]
<strong>Explanation:</strong> The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong>
arr1 = [
{&quot;id&quot;: 1, &quot;b&quot;: {&quot;b&quot;: 94},&quot;v&quot;: [4, 3], &quot;y&quot;: 48}
]
arr2 = [
{&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3]}
]
<strong>Output:</strong> [
{&quot;id&quot;: 1, &quot;b&quot;: {&quot;c&quot;: 84}, &quot;v&quot;: [1, 3], &quot;y&quot;: 48}
]
<strong>Explanation:</strong> The two objects with id=1 are merged together. For the keys &quot;b&quot; and &quot;v&quot; the values from arr2 are used. Since the key &quot;y&quot; only exists in arr1, that value is taken form arr1.</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>arr1 and arr2 are valid JSON arrays</code></li>
<li><code>Each object in arr1 and arr2 has a unique&nbsp;integer id key</code></li>
<li><code>2 &lt;= JSON.stringify(arr1).length &lt;= 10<sup>6</sup></code></li>
<li><code>2 &lt;= JSON.stringify(arr2).length &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@ -0,0 +1,45 @@
<p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can do the following:</p>
<ul>
<li>Select any non-empty substring of <code>s</code>, possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, &#39;b&#39; is converted to &#39;a&#39;, and &#39;a&#39; is converted to &#39;z&#39;.</li>
</ul>
<p>Return <em>the <strong>lexicographically smallest</strong> string you can obtain after performing the above operation <strong>exactly once</strong>.</em></p>
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
A string <code>x</code> is <strong>lexicographically smaller</strong> than a string <code>y</code> of the same length if <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order for the first position <code>i</code> such that <code>x[i] != y[i]</code>.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;cbabc&quot;
<strong>Output:</strong> &quot;baabc&quot;
<strong>Explanation:</strong> We apply the operation on the substring starting at index 0, and ending at index 1 inclusive.
It can be proven that the resulting string is the lexicographically smallest.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;acbbc&quot;
<strong>Output:</strong> &quot;abaab&quot;
<strong>Explanation:</strong> We apply the operation on the substring starting at index 1, and ending at index 4 inclusive.
It can be proven that the resulting string is the lexicographically smallest.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;leetcode&quot;
<strong>Output:</strong> &quot;kddsbncd&quot;
<strong>Explanation:</strong> We apply the operation on the entire string.
It can be proven that the resulting string is the lexicographically smallest.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of lowercase English letters</li>
</ul>

Some files were not shown because too many files have changed in this diff Show More