mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
b14260345b
commit
6465d37d92
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.03.26**
|
||||
> 最后更新日期: **2023.04.14**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
85
leetcode-cn/originData/[no content]beautiful-pairs.json
Normal file
85
leetcode-cn/originData/[no content]beautiful-pairs.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
62
leetcode-cn/originData/flatten-deeply-nested-array.json
Normal file
62
leetcode-cn/originData/flatten-deeply-nested-array.json
Normal file
@ -0,0 +1,62 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2759",
|
||||
"questionFrontendId": "2625",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2222276,
|
||||
"title": "Flatten Deeply Nested Array",
|
||||
"titleSlug": "flatten-deeply-nested-array",
|
||||
"content": "<p>Write a function that accepts a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, and returns a <strong>flattened</strong> version of that array.</p>\r\n\r\n<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>\r\n\r\n<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be 0.</p>\r\n\r\n<p>Please solve it without the built-in <code>Array.flat</code> method.</p>\r\n\r\n<p> </p>\r\n<p><strong class=\"example\">Example 1:</strong></p>\r\n\r\n<pre>\r\n<strong>Input</strong>\r\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\nn = 0\r\n<strong>Output</strong>\r\n[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\n\r\n<strong>Explanation</strong>\r\nPassing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>\r\n\r\n<p><strong class=\"example\">Example 2:</strong></p>\r\n\r\n<pre>\r\n<strong>Input</strong>\r\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\nn = 1\r\n<strong>Output</strong>\r\n[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]\r\n\r\n<strong>Explanation</strong>\r\nThe subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.</pre>\r\n\r\n<p><strong class=\"example\">Example 3:</strong></p>\r\n\r\n<pre>\r\n<strong>Input</strong>\r\narr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\nn = 2\r\n<strong>Output</strong>\r\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\r\n\r\n<strong>Explanation</strong>\r\nThe maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>\r\n\r\n<p> </p>\r\n<p><strong>Constraints:</strong></p>\r\n\r\n<ul>\r\n\t<li><code>0 <= count of numbers in arr <= 10<sup>5</sup></code></li>\r\n\t<li><code>0 <= count of subarrays in arr <= 10<sup>5</sup></code></li>\r\n\t<li><code>maxDepth <= 1000</code></li>\r\n\t<li><code>-1000 <= each number <= 1000</code></li>\r\n\t<li><code><font face=\"monospace\">0 <= n <= 1000</font></code></li>\r\n</ul>",
|
||||
"translatedTitle": "扁平化嵌套数组",
|
||||
"translatedContent": "<p>请你编写一个函数,它接收一个 <strong>多维数组 </strong><code>arr</code> 和它的深度 <code>n</code> ,并返回该数组的 <strong>扁平化 </strong>后的结果。</p>\n\n<p><strong>多维数组 </strong>是一种包含整数或其他 <strong>多维数组 </strong>的递归数据结构。</p>\n\n<p>数组 <strong>扁平化</strong> 是对数组的一种操作,定义是将原数组部分或全部子数组删除,并替换为该子数组中的实际元素。只有当嵌套的数组深度大于 <code>n</code> 时,才应该执行扁平化操作。第一层数组中元素的深度被认为是 0。</p>\n\n<p>请在没有使用内置方法 <code>Array.flat</code> 的前提下解决这个问题。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<strong>输入</strong>\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 0\n<strong>输出</strong>\n[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\n\n<strong>解释</strong>\n传递深度 n=0 的多维数组将始终得到原始数组。这是因为 子数组(0) 的最小可能的深度不小于 n=0 。因此,任何子数组都不应该被平面化。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<strong>输入</strong>\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 1\n<strong>输出</strong>\n[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]\n\n<strong>解释</strong>\n以 4 、7 和 13 开头的子数组都被扁平化了,这是因为它们的深度为 0 , 而 0 小于 1 。然而 [9,10,11] 其深度为 1 ,所以未被扁平化。</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<strong>输入</strong>\narr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 2\n<strong>输出</strong>\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\n\n<strong>解释</strong>\n所有子数组的最大深度都为 1 。因此,它们都被扁平化了。</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr 的元素个数 <= 10<sup>5</sup></code></li>\n\t<li><code>0 <= arr 的子数组个数 <= 10<sup>5</sup></code></li>\n\t<li><code>maxDepth <= 1000</code></li>\n\t<li><code>-1000 <= each number <= 1000</code></li>\n\t<li><code><font face=\"monospace\">0 <= n <= 1000</font></code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"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 {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type MultiDimensionalArray = (number | MultiDimensionalArray)[];\n\nvar flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"2\", \"totalSubmission\": \"2\", \"totalAcceptedRaw\": 2, \"totalSubmissionRaw\": 2, \"acRate\": \"100.0%\"}",
|
||||
"hints": [
|
||||
"Write a recursive function that keeps track of the current depth.",
|
||||
"if the current depth >= the maximum depth, always just push the value to the returned array. Otherwise recursively call flat on the array."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0",
|
||||
"metaData": "{\n \"classname\": \"Array\",\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true,\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": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n1\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n2",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
189
leetcode-cn/originData/prime-in-diagonal.json
Normal file
189
leetcode-cn/originData/prime-in-diagonal.json
Normal file
File diff suppressed because one or more lines are too long
184
leetcode-cn/originData/sum-of-distances.json
Normal file
184
leetcode-cn/originData/sum-of-distances.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>返回位于 <code>nums</code> 至少一条 <strong>对角线</strong> 上的最大 <strong>质数</strong> 。如果任一对角线上均不存在质数,返回<em> 0 。</em></p>
|
||||
|
||||
<p>注意:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果某个整数大于 <code>1</code> ,且不存在除 <code>1</code> 和自身之外的正整数因子,则认为该整数是一个质数。</li>
|
||||
<li>如果存在整数 <code>i</code> ,使得 <code>nums[i][i] = val</code> 或者 <code>nums[i][nums.length - i - 1]= val</code> ,则认为整数 <code>val</code> 位于 <code>nums</code> 的一条对角线上。</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>在上图中,一条对角线是 <strong>[1,5,9]</strong> ,而另一条对角线是<strong> [3,5,7]</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>输出:</strong>11
|
||||
<strong>解释:</strong>数字 1、3、6、9 和 11 是所有 "位于至少一条对角线上" 的数字。由于 11 是最大的质数,故返回 11 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>输出:</strong>17
|
||||
<strong>解释:</strong>数字 1、3、9、10 和 17 是所有满足"位于至少一条对角线上"的数字。由于 17 是最大的质数,故返回 17 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,58 @@
|
||||
<p>请你编写一个函数,它接收一个 <strong>多维数组 </strong><code>arr</code> 和它的深度 <code>n</code> ,并返回该数组的 <strong>扁平化 </strong>后的结果。</p>
|
||||
|
||||
<p><strong>多维数组 </strong>是一种包含整数或其他 <strong>多维数组 </strong>的递归数据结构。</p>
|
||||
|
||||
<p>数组 <strong>扁平化</strong> 是对数组的一种操作,定义是将原数组部分或全部子数组删除,并替换为该子数组中的实际元素。只有当嵌套的数组深度大于 <code>n</code> 时,才应该执行扁平化操作。第一层数组中元素的深度被认为是 0。</p>
|
||||
|
||||
<p>请在没有使用内置方法 <code>Array.flat</code> 的前提下解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 0
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
<strong>解释</strong>
|
||||
传递深度 n=0 的多维数组将始终得到原始数组。这是因为 子数组(0) 的最小可能的深度不小于 n=0 。因此,任何子数组都不应该被平面化。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 1
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
|
||||
|
||||
<strong>解释</strong>
|
||||
以 4 、7 和 13 开头的子数组都被扁平化了,这是因为它们的深度为 0 , 而 0 小于 1 。然而 [9,10,11] 其深度为 1 ,所以未被扁平化。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 2
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
|
||||
|
||||
<strong>解释</strong>
|
||||
所有子数组的最大深度都为 1 。因此,它们都被扁平化了。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr 的元素个数 <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= arr 的子数组个数 <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxDepth <= 1000</code></li>
|
||||
<li><code>-1000 <= each number <= 1000</code></li>
|
||||
<li><code><font face="monospace">0 <= n <= 1000</font></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>p</code> 。请你从 <code>nums</code> 中找到 <code>p</code> 个下标对,每个下标对对应数值取差值,你需要使得这 <code>p</code> 个差值的 <strong>最大值</strong> <strong>最小</strong>。同时,你需要确保每个下标在这 <code>p</code> 个下标对中最多出现一次。</p>
|
||||
|
||||
<p>对于一个下标对 <code>i</code> 和 <code>j</code> ,这一对的差值为 <code>|nums[i] - nums[j]|</code> ,其中 <code>|x|</code> 表示 <code>x</code> 的 <strong>绝对值</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>p</code> 个下标对对应数值 <strong>最大差值</strong> 的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [10,1,2,7,1,3], p = 2
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>第一个下标对选择 1 和 4 ,第二个下标对选择 2 和 5 。
|
||||
最大差值为 max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1 。所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,2,1,2], p = 1
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>选择下标 1 和 3 构成下标对。差值为 |2 - 2| = 0 ,这是最大差值的最小值。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/等值距离和 [sum-of-distances].html
Normal file
35
leetcode-cn/problem (Chinese)/等值距离和 [sum-of-distances].html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。现有一个长度等于 <code>nums.length</code> 的数组 <code>arr</code> 。对于满足 <code>nums[j] == nums[i]</code> 且 <code>j != i</code> 的所有 <code>j</code> ,<code>arr[i]</code> 等于所有 <code>|i - j|</code> 之和。如果不存在这样的 <code>j</code> ,则令 <code>arr[i]</code> 等于 <code>0</code> 。</p>
|
||||
|
||||
<p>返回数组<em> </em><code>arr</code><em> 。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,1,1,2]
|
||||
<strong>输出:</strong>[5,0,3,4,0]
|
||||
<strong>解释:</strong>
|
||||
i = 0 ,nums[0] == nums[2] 且 nums[0] == nums[3] 。因此,arr[0] = |0 - 2| + |0 - 3| = 5 。
|
||||
i = 1 ,arr[1] = 0 因为不存在值等于 3 的其他下标。
|
||||
i = 2 ,nums[2] == nums[0] 且 nums[2] == nums[3] 。因此,arr[2] = |2 - 0| + |2 - 3| = 3 。
|
||||
i = 3 ,nums[3] == nums[0] 且 nums[3] == nums[2] 。因此,arr[3] = |3 - 0| + |3 - 2| = 4 。
|
||||
i = 4 ,arr[4] = 0 因为不存在值等于 2 的其他下标。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,5,3]
|
||||
<strong>输出:</strong>[0,0,0]
|
||||
<strong>解释:</strong>因为 nums 中的元素互不相同,对于所有 i ,都有 arr[i] = 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的 <code>m x n</code> 整数矩阵 <code>grid</code> 。你一开始的位置在 <strong>左上角</strong> 格子 <code>(0, 0)</code> 。</p>
|
||||
|
||||
<p>当你在格子 <code>(i, j)</code> 的时候,你可以移动到以下格子之一:</p>
|
||||
|
||||
<ul>
|
||||
<li>满足 <code>j < k <= grid[i][j] + j</code> 的格子 <code>(i, k)</code> (向右移动),或者</li>
|
||||
<li>满足 <code>i < k <= grid[i][j] + i</code> 的格子 <code>(k, j)</code> (向下移动)。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回到达 <strong>右下角</strong> 格子 <code>(m - 1, n - 1)</code> 需要经过的最少移动格子数,如果无法到达右下角格子,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>上图展示了到达右下角格子经过的 4 个格子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>上图展示了到达右下角格子经过的 3 个格子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[2,1,0],[1,0,0]]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>无法到达右下角格子。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a 0-indexed two-dimensional integer array <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the largest <strong>prime</strong> number that lies on at least one of the <b>diagonals</b> of </em><code>nums</code>. In case, no prime is present on any of the diagonals, return<em> 0.</em></p>
|
||||
|
||||
<p>Note that:</p>
|
||||
|
||||
<ul>
|
||||
<li>An integer is <strong>prime</strong> if it is greater than <code>1</code> and has no positive integer divisors other than <code>1</code> and itself.</li>
|
||||
<li>An integer <code>val</code> is on one of the <strong>diagonals</strong> of <code>nums</code> if there exists an integer <code>i</code> for which <code>nums[i][i] = val</code> or an <code>i</code> for which <code>nums[i][nums.length - i - 1] = val</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>In the above diagram, one diagonal is <strong>[1,5,9]</strong> and another diagonal is<strong> [3,5,7]</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="font-size: 10.8333px;">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,55 @@
|
||||
<p>Write a function that accepts a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, and returns a <strong>flattened</strong> version of that array.</p>
|
||||
|
||||
<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>
|
||||
|
||||
<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be 0.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.flat</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 0
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 1
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 2
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
The maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= count of numbers in arr <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= count of subarrays in arr <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxDepth <= 1000</code></li>
|
||||
<li><code>-1000 <= each number <= 1000</code></li>
|
||||
<li><code><font face="monospace">0 <= n <= 1000</font></code></li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once amongst the <code>p</code> pairs.</p>
|
||||
|
||||
<p>Note that for a pair of elements at the index <code>i</code> and <code>j</code>, the difference of this pair is <code>|nums[i] - nums[j]|</code>, where <code>|x|</code> represents the <strong>absolute</strong> <strong>value</strong> of <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> <strong>maximum</strong> difference among all </em><code>p</code> <em>pairs.</em> We define the maximum of an empty set to be zero.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,1,2,7,1,3], p = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
|
||||
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,1,2], p = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. There exists an array <code>arr</code> of length <code>nums.length</code>, where <code>arr[i]</code> is the sum of <code>|i - j|</code> over all <code>j</code> such that <code>nums[j] == nums[i]</code> and <code>j != i</code>. If there is no such <code>j</code>, set <code>arr[i]</code> to be <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>arr</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,1,2]
|
||||
<strong>Output:</strong> [5,0,3,4,0]
|
||||
<strong>Explanation:</strong>
|
||||
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
|
||||
When i = 1, arr[1] = 0 because there is no other index with value 3.
|
||||
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
|
||||
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
|
||||
When i = 4, arr[4] = 0 because there is no other index with value 2.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,5,3]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> Since each element in nums is distinct, arr[i] = 0 for all i.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p>
|
||||
|
||||
<p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p>
|
||||
|
||||
<ul>
|
||||
<li>Cells <code>(i, k)</code> with <code>j < k <= grid[i][j] + j</code> (rightward movement), or</li>
|
||||
<li>Cells <code>(k, j)</code> with <code>i < k <= grid[i][j] + i</code> (downward movement).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>The image above shows one of the paths that visits exactly 3 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,1,0],[1,0,0]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proven that no path exists.
|
||||
</pre>
|
||||
|
||||
<p> </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 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
82
leetcode/originData/[no content]beautiful-pairs.json
Normal file
82
leetcode/originData/[no content]beautiful-pairs.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2747",
|
||||
"questionFrontendId": "2635",
|
||||
"boundTopicId": null,
|
||||
"title": "Apply Transform Over Each Element in Array",
|
||||
"titleSlug": "apply-transform-over-each-element-in-array",
|
||||
"content": "<p>Given an integer array <code>arr</code> and a mapping function <code>fn</code>, return a new array with a transformation applied to each element.</p>\n\n<p>The returned array should be created such that <code>returnedArray[i] = fn(arr[i], i)</code>.</p>\n\n<p>Please solve it without the built-in <code>Array.map</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,2,3], fn = function plusone(n) { return n + 1; }\n<strong>Output:</strong> [2,3,4]\n<strong>Explanation:</strong>\nconst newArray = map(arr, plusone); // [2,3,4]\nThe function increases each value in the array by one. \n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,2,3], fn = function plusI(n, i) { return n + i; }\n<strong>Output:</strong> [1,3,5]\n<strong>Explanation:</strong> The function increases each value by the index it resides in.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [10,20,30], fn = function constant() { return 42; }\n<strong>Output:</strong> [42,42,42]\n<strong>Explanation:</strong> The function always returns 42.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr.length <= 1000</code></li>\n\t<li><code><font face=\"monospace\">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>\n\t<li><font face=\"monospace\"><code>fn returns a number</code></font></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 27,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Array Reduce Transformation\", \"titleSlug\": \"array-reduce-transformation\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "function plusone(n) { return n + 1; }\n[1,2,3]\nfunction plusI(n, i) { return n + i; }\n[1,2,3]\nfunction constant(n, i) { return 42; }\n[10,20,30]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number[]} arr\n * @param {Function} fn\n * @return {number[]}\n */\nvar map = function(arr, fn) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function map(arr: number[], fn: (n: number, i: number) => number): number[] {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1183, \"totalSubmissionRaw\": 1290, \"acRate\": \"91.7%\"}",
|
||||
"hints": [
|
||||
"Start by creating an array that will eventually be returned.",
|
||||
"Loop over each element in the passed array. Push fn(arr[i]) to the returned array."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "function plusone(n) { return n + 1; }\n[1,2,3]",
|
||||
"metaData": "{\n \"name\": \"map\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/array-prototype-last.json
Normal file
59
leetcode/originData/array-prototype-last.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2734",
|
||||
"questionFrontendId": "2619",
|
||||
"boundTopicId": null,
|
||||
"title": "Array Prototype Last",
|
||||
"titleSlug": "array-prototype-last",
|
||||
"content": "Write code that enhances all arrays such that you can call the <code>array.last()</code> method on any array and it will return the last element. If there are no elements in the array, it should return <code>-1</code>.\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> Calling nums.last() should return the last element: 3.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = []\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> Because there are no elements, return -1.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr.length <= 1000</code></li>\n\t<li><code>0 <= arr[i] <= 1000</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 70,
|
||||
"dislikes": 3,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Snail Traversal\", \"titleSlug\": \"snail-traversal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[1,2,3]\n[]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "Array.prototype.last = function() {\n \n};\n\n/**\n * const arr = [1, 2, 3];\n * arr.last(); // 3\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "declare global {\n interface Array<T> {\n last(): T | -1;\n }\n}\n\nArray.prototype.last = function() {\n\n};\n\n/**\n * const arr = [1, 2, 3];\n * arr.last(); // 3\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"3.4K\", \"totalSubmission\": \"4.2K\", \"totalAcceptedRaw\": 3421, \"totalSubmissionRaw\": 4234, \"acRate\": \"80.8%\"}",
|
||||
"hints": [
|
||||
"Inside the Array.prototype.last function body, you have access to the \"this\" keyword. \"this\" is equal to the contents of the array in this case.",
|
||||
"You can access elements in the array via this[0], this[1], etc. You can also access properties and method like this.length, this.forEach, etc."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[1,2,3]",
|
||||
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/array-reduce-transformation.json
Normal file
59
leetcode/originData/array-reduce-transformation.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2761",
|
||||
"questionFrontendId": "2626",
|
||||
"boundTopicId": null,
|
||||
"title": "Array Reduce Transformation",
|
||||
"titleSlug": "array-reduce-transformation",
|
||||
"content": "<p>Given an integer array <code>nums</code>, a reducer function <code>fn</code>, and an intial value <code>init</code>, return a <strong>reduced</strong> array.</p>\n\n<p>A <strong>reduced</strong> array is created by applying the following operation: <code>val = fn(init, nums[0])</code>, <code>val = fn(val, nums[1])</code>, <code>val = fn(val, arr[2])</code>, <code>...</code> until every element in the array has been processed. The final value of <code>val</code> is returned.</p>\n\n<p>If the length of the array is 0, it should return <code>init</code>.</p>\n\n<p>Please solve it without using the built-in <code>Array.reduce</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr; }\ninit = 0\n<strong>Output:</strong> 10\n<strong>Explanation:</strong>\ninitially, the value is init=0.\n(0) + nums[0] = 1\n(1) + nums[1] = 3\n(3) + nums[2] = 6\n(6) + nums[3] = 10\nThe final answer is 10.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr * curr; }\ninit = 100\n<strong>Output:</strong> 130\n<strong>Explanation:</strong>\ninitially, the value is init=100.\n(100) + nums[0]^2 = 101\n(101) + nums[1]^2 = 105\n(105) + nums[2]^2 = 114\n(114) + nums[3]^2 = 130\nThe final answer is 130.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = []\nfn = function sum(accum, curr) { return 0; }\ninit = 25\n<strong>Output:</strong> 25\n<strong>Explanation:</strong> For empty arrays, the answer is always init.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= nums.length <= 1000</code></li>\n\t<li><code>0 <= nums[i] <= 1000</code></li>\n\t<li><code>0 <= init <= 1000</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 18,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0\n[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr * curr; }\n100\n[]\nfunction sum(accum, curr) { return 0; }\n25",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type Fn = (accum: number, curr: number) => number\n\nfunction reduce(nums: number[], fn: Fn, init: number): number {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"1.1K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1117, \"totalSubmissionRaw\": 1278, \"acRate\": \"87.4%\"}",
|
||||
"hints": [
|
||||
"Declare a variable \"res\" and set it it equal to the initial value.",
|
||||
"Loop over each value in the array and set \"res\" = fn(res, arr[i])."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0",
|
||||
"metaData": "{\n \"name\": \"reduce\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"init\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\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"
|
||||
}
|
||||
}
|
||||
}
|
63
leetcode/originData/cache-with-time-limit.json
Normal file
63
leetcode/originData/cache-with-time-limit.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2762",
|
||||
"questionFrontendId": "2622",
|
||||
"boundTopicId": null,
|
||||
"title": "Cache With Time Limit",
|
||||
"titleSlug": "cache-with-time-limit",
|
||||
"content": "<p>Write a class that allows getting and setting key-value pairs, however a <strong>time until expiration</strong> is associated with each key.</p>\n\n<p>The class has three public methods:</p>\n\n<p><code>set(key, value, duration)</code>: accepts an integer <code>key</code>, an integer <code>value</code>, and a <code>duration</code> in milliseconds. Once the <code>duration</code> has elapsed, the key should be inaccessible. The method should return <code>true</code> if the same un-expired key already exists and <code>false</code> otherwise. Both the value and duration should be overwritten if the key already exists.</p>\n\n<p><code>get(key)</code>: if an un-expired key exists, it should return the associated value. Otherwise it should return <code>-1</code>.</p>\n\n<p><code>count()</code>: returns the count of un-expired keys.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \n["TimeLimitedCache", "set", "get", "count", "get"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]\n<strong>Output:</strong> [null, false, 42, 1, -1]\n<strong>Explanation:</strong>\nAt t=0, the cache is constructed.\nAt t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.\nAt t=50, key=1 is requested and the value of 42 is returned.\nAt t=50, count() is called and there is one active key in the cache.\nAt t=100, key=1 expires.\nAt t=150, get(1) is called but -1 is returned because the cache is empty.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \n["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]\n[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\n[0, 0, 40, 50, 120, 200, 250]\n<strong>Output:</strong> [null, false, true, 50, 50, -1]\n<strong>Explanation:</strong>\nAt t=0, the cache is constructed.\nAt t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.\nAt t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.\nAt t=50, get(1) is called which returned 50.\nAt t=120, get(1) is called which returned 50.\nAt t=140, key=1 expires.\nAt t=200, get(1) is called but the cache is empty so -1 is returned.\nAt t=250, count() returns 0 because the cache is empty.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= key <= 10<sup>9</sup></code></li>\n\t<li><code>0 <= value <= 10<sup>9</sup></code></li>\n\t<li><code>0 <= duration <= 1000</code></li>\n\t<li><code>total method calls will not exceed 100</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 13,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Debounce\", \"titleSlug\": \"debounce\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Promise Pool\", \"titleSlug\": \"promise-pool\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[\"TimeLimitedCache\", \"set\", \"get\", \"count\", \"get\"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]\n[\"TimeLimitedCache\", \"set\", \"set\", \"get\", \"get\", \"get\", \"count\"]\n[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]\n[0, 0, 40, 50, 120, 200, 250]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "\nvar TimeLimitedCache = function() {\n \n};\n\n/** \n * @param {number} key\n * @param {number} value\n * @param {number} time until expiration in ms\n * @return {boolean} if un-expired key already existed\n */\nTimeLimitedCache.prototype.set = function(key, value, duration) {\n \n};\n\n/** \n * @param {number} key\n * @return {number} value associated with key\n */\nTimeLimitedCache.prototype.get = function(key) {\n \n};\n\n/** \n * @return {number} count of non-expired keys\n */\nTimeLimitedCache.prototype.count = function() {\n \n};\n\n/**\n * Your TimeLimitedCache object will be instantiated and called as such:\n * var obj = new TimeLimitedCache()\n * obj.set(1, 42, 1000); // false\n * obj.get(1) // 42\n * obj.count() // 1\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "class TimeLimitedCache {\n constructor() {\n\n }\n\n set(key: number, value: number, duration: number): boolean {\n\n }\n\n get(key: number): number {\n\n }\n\n\tcount(): number {\n \n }\n}\n\n/**\n * Your TimeLimitedCache object will be instantiated and called as such:\n * var obj = new TimeLimitedCache()\n * obj.set(1, 42, 1000); // false\n * obj.get(1) // 42\n * obj.count() // 1\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"375\", \"totalSubmission\": \"506\", \"totalAcceptedRaw\": 375, \"totalSubmissionRaw\": 506, \"acRate\": \"74.1%\"}",
|
||||
"hints": [
|
||||
"You can delay execution of code with \"ref = setTimeout(fn, delay)\". You can abort the execution with \"clearTimeout(ref)\"",
|
||||
"When storing the values in the cache, also store a reference to the timeout. The timeout should clear the key from the cache after the expiration has elapsed.",
|
||||
"When you set a key that already exists, clear the existing timeout.",
|
||||
"You can delay execution of code with \"ref = setTimeout(fn, delay)\". You can abort the execution with \"clearTimeout(ref)\"",
|
||||
"When storing the values in the cache, also store a reference to the timeout. The timeout should clear the key from the cache after the expiration has elapsed.",
|
||||
"When you set a key that already exists, clear the existing timeout."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[\"TimeLimitedCache\", \"set\", \"get\", \"count\", \"get\"]\n[[], [1, 42, 100], [1], [], [1]]\n[0, 0, 50, 50, 150]",
|
||||
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"methods\",\n \"type\": \"string[]\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"inputs\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"ts\"\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"
|
||||
}
|
||||
}
|
||||
}
|
61
leetcode/originData/check-if-object-instance-of-class.json
Normal file
61
leetcode/originData/check-if-object-instance-of-class.json
Normal file
@ -0,0 +1,61 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2758",
|
||||
"questionFrontendId": "2618",
|
||||
"boundTopicId": null,
|
||||
"title": "Check if Object Instance of Class",
|
||||
"titleSlug": "check-if-object-instance-of-class",
|
||||
"content": "<p>Write a function that checks if a given object is an instance of a given class or superclass.</p>\n\n<p>There are no constraints on the data types that can be passed to the function.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => checkIfInstance(new Date(), Date)\n<strong>Output:</strong> true\n<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }\n<strong>Output:</strong> true\n<strong>Explanation:</strong>\nclass Animal {};\nclass Dog extends Animal {};\ncheckIfInstance(new Dog(), Animal); // true\n\nDog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => checkIfInstance(Date, Date)\n<strong>Output:</strong> false\n<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => checkIfInstance(5, Number)\n<strong>Output:</strong> true\n<strong>Explanation: </strong>5 is a Number. Note that the "instanceof" keyword would return false.\n</pre>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 39,
|
||||
"dislikes": 10,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "() => checkIfInstanceOf(new Date(), Date)\n() => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }\n() => checkIfInstanceOf(Date, Date)\n() => checkIfInstanceOf(5, Number)",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Object} object\n * @param {Function} classFunction\n * @return {boolean}\n */\nvar checkIfInstanceOf = function(obj, classFunction) {\n \n};\n\n/**\n * checkIfInstanceOf(new Date(), Date); // true\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function checkIfInstanceOf(obj: any, classFunction: any): boolean {\n\n};\n\n/**\n * checkIfInstanceOf(new Date(), Date); // true\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"815\", \"totalSubmission\": \"2.9K\", \"totalAcceptedRaw\": 815, \"totalSubmissionRaw\": 2912, \"acRate\": \"28.0%\"}",
|
||||
"hints": [
|
||||
"In Javascript, inheritance is achieved with the prototype chain.",
|
||||
"You can get the prototype of an object with the Object.getPrototypeOf(obj) function. Alternatively, you can code obj['__proto__'].",
|
||||
"You can compare an object's __proto__ with classFunction.prototype.",
|
||||
"Traverse the entire prototype chain until you find a match."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "() => checkIfInstanceOf(new Date(), Date)",
|
||||
"metaData": "{\n \"name\": \"checkIfInstance\",\n \"params\": [\n {\n \"name\": \"func\",\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"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/convert-object-to-json-string.json
Normal file
60
leetcode/originData/convert-object-to-json-string.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2745",
|
||||
"questionFrontendId": "2633",
|
||||
"boundTopicId": null,
|
||||
"title": "Convert Object to JSON String",
|
||||
"titleSlug": "convert-object-to-json-string",
|
||||
"content": "<p>Given an object, return a valid JSON string of that object. You may assume the object only inludes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by <code>Object.keys()</code>.</p>\n\n<p>Please solve it without using the built-in <code>JSON.stringify</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> object = {"y":1,"x":2}\n<strong>Output:</strong> {"y":1,"x":2}\n<strong>Explanation:</strong> \nReturn the JSON representation.\nNote that the order of keys should be the same as the order returned by Object.keys().</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> object = {"a":"str","b":-12,"c":true,"d":null}\n<strong>Output:</strong> {"a":"str","b":-12,"c":true,"d":null}\n<strong>Explanation:</strong>\nThe primitives of JSON are strings, numbers, booleans, and null.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> object = {"key":{"a":1,"b":[{},null,"Hello"]}}\n<strong>Output:</strong> {"key":{"a":1,"b":[{},null,"Hello"]}}\n<strong>Explanation:</strong>\nObjects and arrays can include other objects and arrays.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> object = true\n<strong>Output:</strong> true\n<strong>Explanation:</strong>\nPrimitive types are valid inputs.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>object includes strings, integers, booleans, arrays, objects, and null</code></li>\n\t<li><code>1 <= JSON.stringify(object).length <= 10<sup>5</sup></code></li>\n\t<li><code>maxNestingLevel <= 1000</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 15,
|
||||
"dislikes": 3,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Flatten Deeply Nested Array\", \"titleSlug\": \"flatten-deeply-nested-array\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "{\"y\":1,\"x\":2}\n{\"a\":\"str\",\"b\":-12,\"c\":true,\"d\":null}\n{\"key\":{\"a\":1,\"b\":[{},null,\"Hello\"]}}\ntrue",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {any} object\n * @return {string}\n */\nvar jsonStringify = function(object) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function jsonStringify(object: any): string {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"347\", \"totalSubmission\": \"442\", \"totalAcceptedRaw\": 347, \"totalSubmissionRaw\": 442, \"acRate\": \"78.5%\"}",
|
||||
"hints": [
|
||||
"Consider the 4 possibilities. The object could be an array, an object, a string, or another type.",
|
||||
"Think about the problem recursively. If you know how to convert any sub-data into a string, how could you use it to convert the entire data into a string?",
|
||||
"If the data is a string, it's just the value surrounded by double quotes. If the data is another type, its just String(data). If the data is an array, it's the recursively stringified value of each item separated by commas. If the data is an object, it's a series of key-value pairs where each value is the recursively stringified value."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"y\":1,\"x\":2}",
|
||||
"metaData": "{\n \"name\": \"jsonStringify\",\n \"params\": [\n {\n \"name\": \"object\",\n \"type\": \"string\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/counter.json
Normal file
59
leetcode/originData/counter.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2732",
|
||||
"questionFrontendId": "2620",
|
||||
"boundTopicId": null,
|
||||
"title": "Counter",
|
||||
"titleSlug": "counter",
|
||||
"content": "<p>Given an integer <code>n</code>, return a <code>counter</code> function. This <code>counter</code> function initially returns <code>n</code> and then returns 1 more than the previous value every subsequent time it is called (<code>n</code>, <code>n + 1</code>, <code>n + 2</code>, etc).</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nn = 10 \n["call","call","call"]\n<strong>Output:</strong> [10,11,12]\n<strong>Explanation: \n</strong>counter() = 10 // The first time counter() is called, it returns n.\ncounter() = 11 // Returns 1 more than the previous time.\ncounter() = 12 // Returns 1 more than the previous time.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nn = -2\n["call","call","call","call","call"]\n<strong>Output:</strong> [-2,-1,0,1,2]\n<strong>Explanation:</strong> counter() initially returns -2. Then increases after each sebsequent call.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>-1000<sup> </sup><= n <= 1000</code></li>\n\t<li><code>At most 1000 calls to counter() will be made</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 85,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Function Composition\", \"titleSlug\": \"function-composition\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "10\n[\"call\",\"call\",\"call\"]\n-2\n[\"call\",\"call\",\"call\",\"call\",\"call\"]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number} n\n * @return {Function} counter\n */\nvar createCounter = function(n) {\n return function() {\n \n };\n};\n\n/** \n * const counter = createCounter(10)\n * counter() // 10\n * counter() // 11\n * counter() // 12\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function createCounter(n: number): () => number {\n return function() {\n\n }\n}\n\n\n/** \n * const counter = createCounter(10)\n * counter() // 10\n * counter() // 11\n * counter() // 12\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"4.4K\", \"totalSubmission\": \"5K\", \"totalAcceptedRaw\": 4446, \"totalSubmissionRaw\": 4974, \"acRate\": \"89.4%\"}",
|
||||
"hints": [
|
||||
"In Javascript, a function can return a clojure. A clojure is defined as a function that can access variables declared above it (it's lexical environment).",
|
||||
"A count variable can be initialized in the outer function and mutated in the inner function."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "10\n[\"call\",\"call\",\"call\"]",
|
||||
"metaData": "{\n \"name\": \"createCounter\",\n \"params\": [\n {\n \"name\": \"n\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"string[]\",\n \"name\": \"calls\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/curry.json
Normal file
59
leetcode/originData/curry.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2740",
|
||||
"questionFrontendId": "2632",
|
||||
"boundTopicId": null,
|
||||
"title": "Curry",
|
||||
"titleSlug": "curry",
|
||||
"content": "<p>Given a function <code>fn</code>, return a <strong>curried</strong> version of that function.</p>\n\n<p>A <strong>curried</strong> function is a function that accepts fewer or an equal number of parameters as the original function and returns either another <strong>curried</strong> function or the same value the original function would have returned.</p>\n\n<p>In practical terms, if you called the original function like <code>sum(1,2,3)</code>, you would call the <strong>curried</strong> version like <code>csum(1)(2)(3)<font face=\"sans-serif, Arial, Verdana, Trebuchet MS\">, </font></code><code>csum(1)(2,3)</code>, <code>csum(1,2)(3)</code>, or <code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function should return the same value as the original.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1],[2],[3]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\nThe code being executed is:\nconst curriedSum = curry(fn);\ncurriedSum(1)(2)(3) === 6;\ncurriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1,2],[3]]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\ncurriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[],[],[1,2,3]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\nYou should be able to pass the parameters in any way, including all at once or none at all.\ncurriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function life() { return 42; }\ninputs = [[]]\n<strong>Output:</strong> 42\n<strong>Explanation:</strong>\ncurrying a function that accepts zero parameters should effectively do nothing.\ncurriedLife() === 42\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= inputs.length <= 1000</code></li>\n\t<li><code>0 <= inputs[i][j] <= 10<sup>5</sup></code></li>\n\t<li><code>0 <= fn.length <= 1000</code></li>\n\t<li><code>inputs.flat().length == fn.length</code></li>\n\t<li><code>function parameters explicitly defined</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 15,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Memoize II\", \"titleSlug\": \"memoize-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "function sum(a, b, c) { return a + b + c; }\n[[1],[2],[3]]\nfunction sum(a, b, c) { return a + b + c; }\n[[1,2],[3]]\nfunction sum(a, b, c) { return a + b + c; }\n[[],[],[1,2,3]]\nfunction life() { return 42; }\n[[]]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function} fn\n * @return {Function}\n */\nvar curry = function(fn) {\n return function curried() {\n\n };\n};\n\n/**\n * function sum(a, b) { return a + b; }\n * const csum = curry(sum);\n * csum(1)(2) // 3\n */\n",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function curry(fn: Function): Function {\n return function curried() {\n\n };\n};\n\n/**\n * function sum(a, b) { return a + b; }\n * const csum = curry(sum);\n * csum(1)(2) // 3\n */\n",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"286\", \"totalSubmission\": \"326\", \"totalAcceptedRaw\": 286, \"totalSubmissionRaw\": 326, \"acRate\": \"87.7%\"}",
|
||||
"hints": [
|
||||
"You can access the count of parameters expected to passed into a function with \"fn.length\".",
|
||||
"You can use recursion. If the length of params passed is equal to fn.length, you are done. Just pass those params to fn. Otherwise return a function that is includes the previous passed params plus the new params. The new function should contain a recursive call to curry()."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "function sum(a, b, c) { return a + b + c; }\n[[1],[2],[3]]",
|
||||
"metaData": "{\n \"name\": \"curry\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"inputs\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/debounce.json
Normal file
59
leetcode/originData/debounce.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2743",
|
||||
"questionFrontendId": "2627",
|
||||
"boundTopicId": null,
|
||||
"title": "Debounce",
|
||||
"titleSlug": "debounce",
|
||||
"content": "<p>Given a function <code>fn</code> and a time in milliseconds <code>t</code>, return a <strong>debounced</strong> version of that function.</p>\n\n<p>A <strong>debounced</strong> function is a function whose execution is delayed by <code>t</code> milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also recieve the passed parameters.</p>\n\n<p>For example, let's say <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>60ms</code>, and <code>100ms</code>. The first 2 function calls would be cancelled, and the 3rd function call would be executed at <code>150ms</code>. If instead <code>t = 35ms</code>, The 1st call would be cancelled, the 2nd would be executed at <code>95ms</code>, and the 3rd would be executed at <code>135ms</code>.</p>\n\n<p><img alt=\"Debounce Schematic\" src=\"https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-11048-pm.png\" style=\"width: 800px; height: 242px;\" /></p>\n\n<p>The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.</p>\n\n<p>Please solve it without using lodash's <code>_.debounce()</code> function.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nt = 50\ncalls = [\n {"t": 50, inputs: [1]},\n {"t": 75, inputs: [2]}\n]\n<strong>Output:</strong> [{"t": 125, inputs: [2]}]\n<strong>Explanation:</strong>\nlet start = Date.now();\nfunction log(...inputs) { \n console.log([Date.now() - start, inputs ])\n}\nconst dlog = debounce(log, 50);\nsetTimeout(() => dlog(1), 50);\nsetTimeout(() => dlog(2), 75);\n\nThe 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms\nThe 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nt = 20\ncalls = [\n {"t": 50, inputs: [1]},\n {"t": 100, inputs: [2]}\n]\n<strong>Output:</strong> [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]\n<strong>Explanation:</strong>\nThe 1st call is delayed until 70ms. The inputs were (1).\nThe 2nd call is delayed until 120ms. The inputs were (2).\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nt = 150\ncalls = [\n {"t": 50, inputs: [1, 2]},\n {"t": 300, inputs: [3, 4]},\n {"t": 300, inputs: [5, 6]}\n]\n<strong>Output:</strong> [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]\n<strong>Explanation:</strong>\nThe 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).\nThe 2nd call is cancelled by the 3rd call\nThe 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= t <= 1000</code></li>\n\t<li><code>1 <= calls.length <= 10</code></li>\n\t<li><code>0 <= calls[i].t <= 1000</code></li>\n\t<li><code>0 <= calls[i].inputs.length <= 10</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 13,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Cache With Time Limit\", \"titleSlug\": \"cache-with-time-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "50\n[{\"t\":50,\"inputs\":[1]},{\"t\":75,\"inputs\":[2]}]\n20\n[{\"t\":50,\"inputs\":[1]},{\"t\":100,\"inputs\":[2]}]\n150\n[{\"t\":50,\"inputs\":[1,2]},{\"t\":300,\"inputs\":[3,4]},{\"t\":300,\"inputs\":[5,6]}]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function} fn\n * @param {number} t milliseconds\n * @return {Function}\n */\nvar debounce = function(fn, t) {\n return function(...args) {\n \n }\n};\n\n/**\n * const log = debounce(console.log, 100);\n * log('Hello'); // cancelled\n * log('Hello'); // cancelled\n * log('Hello'); // Logged at t=100ms\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type F = (...p: any[]) => any\n\nfunction debounce(fn: F, t: number): F {\n return function(...args) {\n \n }\n};\n\n/**\n * const log = debounce(console.log, 100);\n * log('Hello'); // cancelled\n * log('Hello'); // cancelled\n * log('Hello'); // Logged at t=100ms\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"351\", \"totalSubmission\": \"379\", \"totalAcceptedRaw\": 351, \"totalSubmissionRaw\": 379, \"acRate\": \"92.6%\"}",
|
||||
"hints": [
|
||||
"You execute code with a delay with \"ref = setTimeout(fn, delay)\". You can abort the execution of that code with \"clearTimeout(ref)\"",
|
||||
"Whenever you call the function, you should abort any existing scheduled code. Then, you should schedule code to be executed after some delay."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "50\n[{\"t\":50,\"inputs\":[1]},{\"t\":75,\"inputs\":[2]}]",
|
||||
"metaData": "{\n \"name\": \"debounce\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\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"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/filter-elements-from-array.json
Normal file
60
leetcode/originData/filter-elements-from-array.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2746",
|
||||
"questionFrontendId": "2634",
|
||||
"boundTopicId": null,
|
||||
"title": "Filter Elements from Array",
|
||||
"titleSlug": "filter-elements-from-array",
|
||||
"content": "<p>Given an integer array <code>arr</code> and a filtering function <code>fn</code>, return a new array with a fewer or equal number of elements.</p>\n\n<p>The returned array should only contain elements where <code>fn(arr[i], i)</code> evaluated to a truthy value.</p>\n\n<p>Please solve it without the built-in <code>Array.filter</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }\n<strong>Output:</strong> [20,30]\n<strong>Explanation:</strong>\nconst newArray = filter(arr, fn); // [20, 30]\nThe function filters out values that are not greater than 10</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }\n<strong>Output:</strong> [1]\n<strong>Explanation:</strong>\nfn can also accept the index of each element\nIn this case, the function removes elements not at index 0\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }\n<strong>Output:</strong> [-2,0,1,2]\n<strong>Explanation:</strong>\nFalsey values such as 0 should be filtered out\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr.length <= 1000</code></li>\n\t<li><code><font face=\"monospace\">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 26,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Array Reduce Transformation\", \"titleSlug\": \"array-reduce-transformation\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "function greaterThan10(n) { return n > 10; }\n[0,10,20,30]\nfunction firstIndex(n, i) { return i === 0; }\n[1,2,3]\nfunction plusOne(n) { return n + 1 }\n[-2,-1,0,1,2]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number[]} arr\n * @param {Function} fn\n * @return {number[]}\n */\nvar filter = function(arr, fn) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function filter(arr: number[], fn: (n: number, i: number) => any): number[] {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.4K\", \"totalAcceptedRaw\": 1224, \"totalSubmissionRaw\": 1442, \"acRate\": \"84.9%\"}",
|
||||
"hints": [
|
||||
"Start by declaring a new array which will eventually be returned.",
|
||||
"In Javascript, there is the concept of \"truthiness\" and \"falsiness\". Values such as 0, undefined, null, and false are falsy. Most values are truthy: 1, {}, [], true, etc. In Javascript, the contents of if-statements don't need to be booleans. You can say \"if ([1,2,3]) {}\", and it's equivalent to saying 'if (true) {}\".",
|
||||
"Loop over each element in the array. If fn(arr[i]) is truthy, push it to the array."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "function greaterThan10(n) { return n > 10; }\n[0,10,20,30]",
|
||||
"metaData": "{\n \"name\": \"filter\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/flatten-deeply-nested-array.json
Normal file
59
leetcode/originData/flatten-deeply-nested-array.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2759",
|
||||
"questionFrontendId": "2625",
|
||||
"boundTopicId": null,
|
||||
"title": "Flatten Deeply Nested Array",
|
||||
"titleSlug": "flatten-deeply-nested-array",
|
||||
"content": "<p>Given a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, return a <strong>flattened</strong> version of that array.</p>\n\n<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>\n\n<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be <code>0</code>.</p>\n\n<p>Please solve it without the built-in <code>Array.flat</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 0\n<strong>Output</strong>\n[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\n\n<strong>Explanation</strong>\nPassing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input</strong>\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 1\n<strong>Output</strong>\n[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]\n\n<strong>Explanation</strong>\nThe subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input</strong>\narr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 2\n<strong>Output</strong>\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\n\n<strong>Explanation</strong>\nThe maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= count of numbers in arr <= 10<sup>5</sup></code></li>\n\t<li><code>0 <= count of subarrays in arr <= 10<sup>5</sup></code></li>\n\t<li><code>maxDepth <= 1000</code></li>\n\t<li><code>-1000 <= each number <= 1000</code></li>\n\t<li><code><font face=\"monospace\">0 <= n <= 1000</font></code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 8,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Convert Object to JSON String\", \"titleSlug\": \"convert-object-to-json-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n1\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n2",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type MultiDimensionalArray = (number | MultiDimensionalArray)[];\n\nvar flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"383\", \"totalSubmission\": \"573\", \"totalAcceptedRaw\": 383, \"totalSubmissionRaw\": 573, \"acRate\": \"66.8%\"}",
|
||||
"hints": [
|
||||
"Write a recursive function that keeps track of the current depth.",
|
||||
"if the current depth >= the maximum depth, always just push the value to the returned array. Otherwise recursively call flat on the array."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0",
|
||||
"metaData": "{\n \"classname\": \"Array\",\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true,\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/function-composition.json
Normal file
59
leetcode/originData/function-composition.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2741",
|
||||
"questionFrontendId": "2629",
|
||||
"boundTopicId": null,
|
||||
"title": "Function Composition",
|
||||
"titleSlug": "function-composition",
|
||||
"content": "<p>Given an array of functions <code>[f<span style=\"font-size: 10.8333px;\">1</span>, f<sub>2</sub>, f<sub>3</sub>, ..., f<sub>n</sub>]</code>, return a new function <code>fn</code> that is the <strong>function composition</strong> of the array of functions.</p>\n\n<p>The <strong>function composition</strong> of <code>[f(x), g(x), h(x)]</code> is <code>fn(x) = f(g(h(x)))</code>.</p>\n\n<p>The <strong>function composition</strong> of an empty list of functions is the <strong>identity function</strong> <code>f(x) = x</code>.</p>\n\n<p>You may assume each function in the array accepts one integer as input and returns one integer as output.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [x => x + 1, x => x * x, x => 2 * x], x = 4\n<strong>Output:</strong> 65\n<strong>Explanation:</strong>\nEvaluating from right to left ...\nStarting with x = 4.\n2 * (4) = 8\n(8) * (8) = 64\n(64) + 1 = 65\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1\n<strong>Output:</strong> 1000\n<strong>Explanation:</strong>\nEvaluating from right to left ...\n10 * (1) = 10\n10 * (10) = 100\n10 * (100) = 1000\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [], x = 42\n<strong>Output:</strong> 42\n<strong>Explanation:</strong>\nThe composition of zero functions is the identity function</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code><font face=\"monospace\">-1000 <= x <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">0 <= functions.length <= 1000</font></code></li>\n\t<li><font face=\"monospace\"><code>all functions accept and return a single integer</code></font></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 18,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Counter\", \"titleSlug\": \"counter\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[x => x + 1, x => x * x, x => 2 * x]\n4\n[x => 10 * x, x => 10 * x, x => 10 * x]\n1\n[]\n42",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function[]} functions\n * @return {Function}\n */\nvar compose = function(functions) {\n\treturn function(x) {\n \n }\n};\n\n/**\n * const fn = compose([x => x + 1, x => 2 * x])\n * fn(4) // 9\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type F = (x: number) => number;\n\nfunction compose(functions: F[]): F {\n\treturn function(x) {\n \n }\n};\n\n/**\n * const fn = compose([x => x + 1, x => 2 * x])\n * fn(4) // 9\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"913\", \"totalSubmission\": \"1K\", \"totalAcceptedRaw\": 913, \"totalSubmissionRaw\": 1022, \"acRate\": \"89.3%\"}",
|
||||
"hints": [
|
||||
"Start by returning a function that takes in a number and returns a number.",
|
||||
"Call each of the functions in the correct order. Each time passing the output of the previous function into the next function."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[x => x + 1, x => x * x, x => 2 * x]\n4",
|
||||
"metaData": "{\n \"name\": \"compose\",\n \"params\": [\n {\n \"name\": \"functions\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"x\"\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"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/group-by.json
Normal file
60
leetcode/originData/group-by.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2742",
|
||||
"questionFrontendId": "2631",
|
||||
"boundTopicId": null,
|
||||
"title": "Group By",
|
||||
"titleSlug": "group-by",
|
||||
"content": "<p>Write code that enhances all arrays such that you can call the <code>array.groupBy(fn)</code> method on any array and it will return a <strong>grouped</strong> version of the array.</p>\n\n<p>A <strong>grouped</strong> array is an object where each key is the output of <code>fn(arr[i])</code> and each value is an array containing all items in the original array with that key.</p>\n\n<p>The provided callback <code>fn</code> will accept an item in the array and return a string key.</p>\n\n<p>The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.</p>\n\n<p>Please solve it without lodash's <code>_.groupBy</code> function.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narray = [\n {"id":"1"},\n {"id":"1"},\n {"id":"2"}\n], \nfn = function (item) { \n return item.id; \n}\n<strong>Output:</strong> \n{ \n "1": [{"id": "1"}, {"id": "1"}], \n "2": [{"id": "2"}] \n}\n<strong>Explanation:</strong>\nOutput is from array.groupBy(fn).\nThe selector function gets the "id" out of each item in the array.\nThere are two objects with an "id" of 1. Both of those objects are put in the first array.\nThere is one object with an "id" of 2. That object is put in the second array.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narray = [\n [1, 2, 3],\n [1, 3, 5],\n [1, 5, 9]\n]\nfn = function (list) { \n return String(list[0]); \n}\n<strong>Output:</strong> \n{ \n "1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]] \n}\n<strong>Explanation:</strong>\nThe array can be of any type. In this case, the selector function defines the key as being the first element in the array. \nAll the arrays have 1 as their first element so they are grouped together.\n{\n "1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]\n}\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nfn = function (n) { \n return String(n > 5);\n}\n<strong>Output:</strong>\n{\n "true": [6, 7, 8, 9, 10],\n "false": [1, 2, 3, 4, 5]\n}\n<strong>Explanation:</strong>\nThe selector function splits the array by whether each number is greater than 5.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= array.length <= 10<sup>5</sup></code></li>\n\t<li><code>fn returns a string</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 8,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Snail Traversal\", \"titleSlug\": \"snail-traversal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Array Reduce Transformation\", \"titleSlug\": \"array-reduce-transformation\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[{\"id\":\"1\"},{\"id\":\"1\"},{\"id\":\"2\"}]\nfunction (item) { return item.id; }\n[[1,2,3],[1,3,5],[1,5,9]]\nfunction (list) { return String(list[0]); }\n[1,2,3,4,5,6,7,8,9,10]\nfunction (n) { return String(n > 5); }",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function} fn\n * @return {Array}\n */\nArray.prototype.groupBy = function(fn) {\n \n};\n\n/**\n * [1,2,3].groupBy(String) // {\"1\":[1],\"2\":[2],\"3\":[3]}\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "declare global {\n interface Array<T> {\n groupBy(fn: (item: T) => string): Record<string, T[]>\n }\n}\n\nArray.prototype.groupBy = function(fn) {\n \n}\n\n/**\n * [1,2,3].groupBy(String) // {\"1\":[1],\"2\":[2],\"3\":[3]}\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"352\", \"totalSubmission\": \"402\", \"totalAcceptedRaw\": 352, \"totalSubmissionRaw\": 402, \"acRate\": \"87.6%\"}",
|
||||
"hints": [
|
||||
"First declare an object that will eventually be returned.",
|
||||
"Iterate of each element in the array. You can access the array with the \"this\" keyword.",
|
||||
"The key is fn(arr[i]). If the key already exists on the object, set the value to be an empty array. Then push the value onto the array at the key."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[{\"id\":\"1\"},{\"id\":\"1\"},{\"id\":\"2\"}]\nfunction (item) { return item.id; }",
|
||||
"metaData": "{\n \"name\": \"groupBy\",\n \"params\": [\n {\n \"name\": \"list\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\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"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/json-deep-equal.json
Normal file
60
leetcode/originData/json-deep-equal.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2735",
|
||||
"questionFrontendId": "2628",
|
||||
"boundTopicId": null,
|
||||
"title": "JSON Deep Equal",
|
||||
"titleSlug": "json-deep-equal",
|
||||
"content": "<p>Given two objects <code>o1</code> and <code>o2</code>, check if they are <strong>deeply equal</strong>.</p>\n\n<p>For two objects to be <strong>deeply equal</strong>, they must contain the same keys, and the associated values must also be <strong>deeply equal</strong>. Two objects are also considered <strong>deeply equal</strong> if they pass the <code>===</code> equality check.</p>\n\n<p>You may assume both objects are the output of <code>JSON.parse</code>. In other words, they are valid JSON.</p>\n\n<p>Please solve it without using lodash's <code>_.isEqual()</code> function.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}\n<strong>Output:</strong> true\n<strong>Explanation:</strong> The keys and values match exactly.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}\n<strong>Output:</strong> true\n<strong>Explanation:</strong> Although the keys are in a different order, they still match exactly.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}\n<strong>Output:</strong> false\n<strong>Explanation:</strong> The array of numbers is different from the array of strings.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> o1 = true, o2 = false\n<strong>Output:</strong> false\n<strong>Explanation:</strong> true !== false</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= JSON.stringify(o1).length <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= JSON.stringify(o2).length <= 10<sup>5</sup></code></li>\n\t<li><code>maxNestingDepth <= 1000</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 13,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Convert Object to JSON String\", \"titleSlug\": \"convert-object-to-json-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Flatten Deeply Nested Array\", \"titleSlug\": \"flatten-deeply-nested-array\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "{\"x\":1,\"y\":2}\n{\"x\":1,\"y\":2}\n{\"y\":2,\"x\":1}\n{\"x\":1,\"y\":2}\n{\"x\":null,\"L\":[1,2,3]}\n{\"x\":null,\"L\":[\"1\",\"2\",\"3\"]}\ntrue\nfalse",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {any} o1\n * @param {any} o2\n * @return {boolean}\n */\nvar areDeeplyEqual = function(o1, o2) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function areDeeplyEqual(o1: any, o2: any): boolean {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"285\", \"totalSubmission\": \"860\", \"totalAcceptedRaw\": 285, \"totalSubmissionRaw\": 860, \"acRate\": \"33.1%\"}",
|
||||
"hints": [
|
||||
"You can check if a value is an array with the Array.isArray() method. You can check if a value is an object by saying typeof obj === 'object' && obj !== null. You can list the keys of an object with the Object.keys() function.",
|
||||
"If two objects have different keys or two arrays have a different length, they cannot be equal.",
|
||||
"You can use recursion to investigate if the values of an object or array are also deeply equal. The base case is when the values are primitives (string, number, etc), at which case the check is a trivial === check."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"x\":1,\"y\":2}\n{\"x\":1,\"y\":2}",
|
||||
"metaData": "{\n \"name\": \"areDeeplyEqual\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"o1\"\n },\n {\n \"type\": \"string\",\n \"name\": \"o2\"\n }\n ],\n \"return\": {\n \"type\": \"boolean\"\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"
|
||||
}
|
||||
}
|
||||
}
|
61
leetcode/originData/memoize-ii.json
Normal file
61
leetcode/originData/memoize-ii.json
Normal file
@ -0,0 +1,61 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2744",
|
||||
"questionFrontendId": "2630",
|
||||
"boundTopicId": null,
|
||||
"title": "Memoize II",
|
||||
"titleSlug": "memoize-ii",
|
||||
"content": "<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>\n\n<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will return a cached value.</p>\n\n<p><code>fn</code> can be any function and there are no constraints on what type of values it accepts. Inputs are considered identical if they are <code>===</code> to each other.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \ngetInputs = () => [[2,2],[2,2],[1,2]]\nfn = function (a, b) { return a + b; }\n<strong>Output:</strong> [{"val":4,"calls":1},{"val":4,"calls":1},{"val":3,"calls":2}]\n<strong>Explanation:</strong>\nconst inputs = getInputs();\nconst memoized = memoize(fn);\nfor (const arr of inputs) {\n memoized(...arr);\n}\n\nFor the inputs of (2, 2): 2 + 2 = 4, and it required a call to fn().\nFor the inputs of (2, 2): 2 + 2 = 4, but those inputs were seen before so no call to fn() was required.\nFor the inputs of (1, 2): 1 + 2 = 3, and it required another call to fn() for a total of 2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \ngetInputs = () => [[{},{}],[{},{}],[{},{}]] \nfn = function (a, b) { return ({...a, ...b}); }\n<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":2},{"val":{},"calls":3}]\n<strong>Explanation:</strong>\nMerging two empty objects will always result in an empty object. It may seem like there should only be 1 call to fn() because of cache-hits, however none of those objects are === to each other.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \ngetInputs = () => { const o = {}; return [[o,o],[o,o],[o,o]]; }\nfn = function (a, b) { return ({...a, ...b}); }\n<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":1},{"val":{},"calls":1}]\n<strong>Explanation:</strong>\nMerging two empty objects will always result in an empty object. The 2nd and 3rd third function calls result in a cache-hit. This is because every object passed in is identical.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= inputs.length <= 10<sup>5</sup></code></li>\n\t<li><code>0 <= inputs.flat().length <= 10<sup>5</sup></code></li>\n\t<li><code>inputs[i][j] != NaN</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Hard",
|
||||
"likes": 11,
|
||||
"dislikes": 4,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Curry\", \"titleSlug\": \"curry\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "() => [[2,2],[2,2],[1,2]]\nfunction (a, b) { return a + b; }\n() => [[{},{}],[{},{}],[{},{}]]\nfunction (a, b) { return ({...a, ...b}); }\n() => { const o = {}; return [[o,o],[o,o],[o,o]]; }\nfunction (a, b) { return ({...a, ...b}); }",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function} fn\n */\nfunction memoize(fn) {\n return function() {\n \n }\n}\n\n\n/** \n * let callCount = 0;\n * const memoizedFn = memoize(function (a, b) {\n *\t callCount += 1;\n * return a + b;\n * })\n * memoizedFn(2, 3) // 5\n * memoizedFn(2, 3) // 5\n * console.log(callCount) // 1 \n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type Fn = (...params: any) => any\n\nfunction memoize(fn: Fn): Fn {\n return function() {\n \n }\n}\n\n\n/** \n * let callCount = 0;\n * const memoizedFn = memoize(function (a, b) {\n *\t callCount += 1;\n * return a + b;\n * })\n * memoizedFn(2, 3) // 5\n * memoizedFn(2, 3) // 5\n * console.log(callCount) // 1 \n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"225\", \"totalSubmission\": \"536\", \"totalAcceptedRaw\": 225, \"totalSubmissionRaw\": 536, \"acRate\": \"42.0%\"}",
|
||||
"hints": [
|
||||
"Just because JSON.stringify(obj1) === JSON.stringify(obj2), doesn't necessarily mean obj1 === obj2.",
|
||||
"You could iterate over all previously passed inputs to check if there has been a match. However, that will be very slow.",
|
||||
"Javascript Maps are a could way to associate arbitrary data.",
|
||||
"Make a tree structure of Maps. The depth of the tree should match the number of input parameters."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "() => [[2,2],[2,2],[1,2]]\nfunction (a, b) { return a + b; }",
|
||||
"metaData": "{\n \"name\": \"memoize\",\n \"params\": [\n {\n \"name\": \"getInputs\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\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"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/memoize.json
Normal file
60
leetcode/originData/memoize.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2731",
|
||||
"questionFrontendId": "2623",
|
||||
"boundTopicId": null,
|
||||
"title": "Memoize",
|
||||
"titleSlug": "memoize",
|
||||
"content": "<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>\n\n<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will returned a cached value.</p>\n\n<p>You can assume there are <strong>3 </strong>possible input functions: <code>sum</code><strong>, </strong><code>fib</code><strong>, </strong>and <code>factorial</code><strong>.</strong></p>\n\n<ul>\n\t<li><code>sum</code><strong> </strong>accepts two integers <code>a</code> and <code>b</code> and returns <code>a + b</code>.</li>\n\t<li><code>fib</code><strong> </strong>accepts a single integer <code>n</code> and returns <code>1</code> if <font face=\"monospace\"><code>n <= 1</code> </font>or<font face=\"monospace\"> <code>fib(n - 1) + fib(n - 2)</code> </font>otherwise.</li>\n\t<li><code>factorial</code> accepts a single integer <code>n</code> and returns <code>1</code> if <code>n <= 1</code> or <code>factorial(n - 1) * n</code> otherwise.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n"sum"\n["call","call","getCallCount","call","getCallCount"]\n[[2,2],[2,2],[],[1,2],[]]\n<strong>Output</strong>\n[4,4,1,3,2]\n\n<strong>Explanation</strong>\nconst sum = (a, b) => a + b;\nconst memoizedSum = memoize(sum);\nmemoizedSum(2, 2); // Returns 4. sum() was called as (2, 2) was not seen before.\nmemoizedSum(2, 2); // Returns 4. However sum() was not called because the same inputs were seen before.\n// Total call count: 1\nmemoizedSum(1, 2); // Returns 3. sum() was called as (1, 2) was not seen before.\n// Total call count: 2\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input\n</strong>"factorial"\n["call","call","call","getCallCount","call","getCallCount"]\n[[2],[3],[2],[],[3],[]]\n<strong>Output</strong>\n[2,6,2,2,6,2]\n\n<strong>Explanation</strong>\nconst factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));\nconst memoFactorial = memoize(factorial);\nmemoFactorial(2); // Returns 2.\nmemoFactorial(3); // Returns 6.\nmemoFactorial(2); // Returns 2. However factorial was not called because 2 was seen before.\n// Total call count: 2\nmemoFactorial(3); // Returns 6. However factorial was not called because 3 was seen before.\n// Total call count: 2\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input\n</strong>"fib"\n["call","getCallCount"]\n[[5],[]]\n<strong>Output</strong>\n[8,1]\n\n<strong>Explanation\n</strong>fib(5) = 8\n// Total call count: 1\n\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= a, b <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= n <= 10</code></li>\n\t<li><code>at most 10<sup>5</sup> function calls</code></li>\n\t<li><code>at most 10<sup>5</sup> attempts to access callCount</code></li>\n\t<li><code>input function is sum, fib, or factorial</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 18,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Counter\", \"titleSlug\": \"counter\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Curry\", \"titleSlug\": \"curry\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Function Composition\", \"titleSlug\": \"function-composition\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Memoize II\", \"titleSlug\": \"memoize-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "\"sum\"\n[\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\n[[2,2],[2,2],[],[1,2],[]]\n\"factorial\"\n[\"call\",\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\n[[2],[3],[2],[],[3],[]]\n\"fib\"\n[\"call\",\"getCallCount\"]\n[[5],[]]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function} fn\n */\nfunction memoize(fn) {\n return function(...args) {\n \n }\n}\n\n\n/** \n * let callCount = 0;\n * const memoizedFn = memoize(function (a, b) {\n *\t callCount += 1;\n * return a + b;\n * })\n * memoizedFn(2, 3) // 5\n * memoizedFn(2, 3) // 5\n * console.log(callCount) // 1 \n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type Fn = (...params: any) => any\n\nfunction memoize(fn: Fn): Fn {\n return function(...args) {\n \n }\n}\n\n\n/** \n * let callCount = 0;\n * const memoizedFn = memoize(function (a, b) {\n *\t callCount += 1;\n * return a + b;\n * })\n * memoizedFn(2, 3) // 5\n * memoizedFn(2, 3) // 5\n * console.log(callCount) // 1 \n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"576\", \"totalSubmission\": \"919\", \"totalAcceptedRaw\": 576, \"totalSubmissionRaw\": 919, \"acRate\": \"62.7%\"}",
|
||||
"hints": [
|
||||
"You can create copy of a function by spreading function parameters. \r\n\r\nfunction outerFunction(passedFunction) {\r\n return newFunction(...params) {\r\n return passedFunction(...params);\r\n };\r\n}",
|
||||
"params is an array. Since you know all values in the array are numbers, you can turn it into a string with JSON.stringify().",
|
||||
"In the outerFunction, you can declare a Map or Object. In the inner function you can avoid executing the passed function if the params have already been passed before."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "\"sum\"\n[\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\n[[2,2],[2,2],[],[1,2],[]]",
|
||||
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"fn_name\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string[]\",\n \"name\": \"methods\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"args\"\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
186
leetcode/originData/prime-in-diagonal.json
Normal file
186
leetcode/originData/prime-in-diagonal.json
Normal file
File diff suppressed because one or more lines are too long
59
leetcode/originData/promise-pool.json
Normal file
59
leetcode/originData/promise-pool.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2750",
|
||||
"questionFrontendId": "2636",
|
||||
"boundTopicId": null,
|
||||
"title": "Promise Pool",
|
||||
"titleSlug": "promise-pool",
|
||||
"content": "<p>Given an array of asyncronous functions <code>functions</code> and a <strong>pool limit</strong> <code>n</code>, return an asyncronous function <code>promisePool</code>. It should return a promise that resolves when all the input functions resolve.</p>\n\n<p><b>Pool limit</b> is defined as the maximum number promises that can be pending at once. <code>promisePool</code> should begin execution of as many functions as possible and continue executing new functions when old promises resolve. <code>promisePool</code> should execute <code>functions[i]</code> then <code>functions[i + 1]</code> then <code>functions[i + 2]</code>, etc. When the last promise resolves, <code>promisePool</code> should also resolve.</p>\n\n<p>For example, if <code>n = 1</code>, <code>promisePool</code> will execute one function at a time in series. However, if <code>n = 2</code>, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.</p>\n\n<p>You can assume all <code>functions</code> never reject. It is acceptable for <code>promisePool</code> to return a promise that resolves any value.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfunctions = [\n () => new Promise(res => setTimeout(res, 300)),\n () => new Promise(res => setTimeout(res, 400)),\n () => new Promise(res => setTimeout(res, 200))\n]\nn = 2\n<strong>Output:</strong> [[300,400,500],500]\n<strong>Explanation:</strong>\nThree functions are passed in. They sleep for 300ms, 400ms, and 200ms respectively.\nAt t=0, the first 2 functions are executed. The pool size limit of 2 is reached.\nAt t=300, the 1st function resolves, and the 3rd function is executed. Pool size is 2.\nAt t=400, the 2nd function resolves. There is nothing left to execute. Pool size is 1.\nAt t=500, the 3rd function resolves. Pool size is zero so the returned promise also resolves.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:\n</strong>functions = [\n () => new Promise(res => setTimeout(res, 300)),\n () => new Promise(res => setTimeout(res, 400)),\n () => new Promise(res => setTimeout(res, 200))\n]\nn = 5\n<strong>Output:</strong> [[300,400,200],400]\n<strong>Explanation:</strong>\nAt t=0, all 3 functions are executed. The pool limit of 5 is never met.\nAt t=200, the 3rd function resolves. Pool size is 2.\nAt t=300, the 1st function resolved. Pool size is 1.\nAt t=400, the 2nd function resolves. Pool size is 0, so the returned promise also resolves.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfunctions = [\n () => new Promise(res => setTimeout(res, 300)),\n () => new Promise(res => setTimeout(res, 400)),\n () => new Promise(res => setTimeout(res, 200))\n]\nn = 1\n<strong>Output:</strong> [[300,700,900],900]\n<strong>Explanation:</strong>\nAt t=0, the 1st function is executed. Pool size is 1.\nAt t=300, the 1st function resolves and the 2nd function is executed. Pool size is 1.\nAt t=700, the 2nd function resolves and the 3rd function is executed. Pool size is 1.\nAt t=900, the 3rd function resolves. Pool size is 0 so the returned promise resolves.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= functions.length <= 10</code></li>\n\t<li><code><font face=\"monospace\">1 <= n <= 10</font></code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 8,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Sleep\", \"titleSlug\": \"sleep\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Cache With Time Limit\", \"titleSlug\": \"cache-with-time-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n2\n[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n5\n[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n1",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function[]} functions\n * @param {number} n\n * @return {Function}\n */\nvar promisePool = async function(functions, n) {\n\n};\n\n/**\n * const sleep = (t) => new Promise(res => setTimeout(res, t));\n * promisePool([() => sleep(500), () => sleep(400)], 1)\n * .then(console.log) // After 900ms\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type F = () => Promise<any>;\n\nfunction promisePool(functions: F[], n: number): Promise<any> {\n\n};\n\n/**\n * const sleep = (t) => new Promise(res => setTimeout(res, t));\n * promisePool([() => sleep(500), () => sleep(400)], 1)\n * .then(console.log) // After 900ms\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"180\", \"totalSubmission\": \"223\", \"totalAcceptedRaw\": 180, \"totalSubmissionRaw\": 223, \"acRate\": \"80.7%\"}",
|
||||
"hints": [
|
||||
"Initially execute all the functions until the queue fills up.",
|
||||
"Every time a function resolves, add a new promise to the queue if possible."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[() => new Promise(res => setTimeout(res, 300)), () => new Promise(res => setTimeout(res, 400)), () => new Promise(res => setTimeout(res, 200))]\n2",
|
||||
"metaData": "{\n \"name\": \"promisePool\",\n \"params\": [\n {\n \"name\": \"getFunctions\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"n\"\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"
|
||||
}
|
||||
}
|
||||
}
|
63
leetcode/originData/promise-time-limit.json
Normal file
63
leetcode/originData/promise-time-limit.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2749",
|
||||
"questionFrontendId": "2637",
|
||||
"boundTopicId": null,
|
||||
"title": "Promise Time Limit",
|
||||
"titleSlug": "promise-time-limit",
|
||||
"content": "<p>Given an asyncronous function <code>fn</code> and a time <code>t</code> in milliseconds, return a new <strong>time limited</strong> version of the input function.</p>\n\n<p>A <strong>time limited</strong> function is a function that is identical to the original unless it takes longer than <code>t</code> milliseconds to fullfill. In that case, it will reject with <code>"Time Limit Exceeded"</code>. Note that it should reject with a string, not an <code>Error</code>.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 50\n<strong>Output:</strong> {"rejected":"Time Limit Exceeded","time":50}\n<strong>Explanation:</strong>\nThe provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (n) => { \n await new Promise(res => setTimeout(res, 100)); \n return n * n; \n}\ninputs = [5]\nt = 150\n<strong>Output:</strong> {"resolved":25,"time":100}\n<strong>Explanation:</strong>\nThe function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async (a, b) => { \n await new Promise(res => setTimeout(res, 120)); \n return a + b; \n}\ninputs = [5,10]\nt = 150\n<strong>Output:</strong> {"resolved":15,"time":120}\n<strong>Explanation:</strong>\nThe function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = async () => { \n throw "Error";\n}\ninputs = []\nt = 1000\n<strong>Output:</strong> {"rejected":"Error","time":0}\n<strong>Explanation:</strong>\nThe function immediately throws an error.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= inputs.length <= 10</code></li>\n\t<li><code>0 <= t <= 1000</code></li>\n\t<li><code>fn returns a promise</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 24,
|
||||
"dislikes": 3,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Sleep\", \"titleSlug\": \"sleep\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Debounce\", \"titleSlug\": \"debounce\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Promise Pool\", \"titleSlug\": \"promise-pool\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Cache With Time Limit\", \"titleSlug\": \"cache-with-time-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n50\nasync (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n150\nasync (a, b) => { await new Promise(res => setTimeout(res, 120)); return a + b; }\n[5,10]\n150\nasync () => { throw \"Error\"; }\n[]\n1000",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Function} fn\n * @param {number} t\n * @return {Function}\n */\nvar timeLimit = function(fn, t) {\n\treturn async function(...args) {\n \n }\n};\n\n/**\n * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);\n * limited(150).catch(console.log) // \"Time Limit Exceeded\" at t=100ms\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type Fn = (...params: any[]) => Promise<any>;\n\nfunction timeLimit(fn: Fn, t: number): Fn {\n\treturn async function(...args) {\n \n }\n};\n\n/**\n * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);\n * limited(150).catch(console.log) // \"Time Limit Exceeded\" at t=100ms\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"494\", \"totalSubmission\": \"593\", \"totalAcceptedRaw\": 494, \"totalSubmissionRaw\": 593, \"acRate\": \"83.3%\"}",
|
||||
"hints": [
|
||||
"You can return a copy of a function with: \r\n\r\nfunction outerFunction(fn) { \r\n return function innerFunction(...params) {\r\n return fn(...params);\r\n };\r\n}",
|
||||
"Inside the inner function, you will need to return a new Promise.",
|
||||
"You can create a new promise like: new Promise((resolve, reject) => {}).",
|
||||
"You can execute code with a delay with \"setTimeout(fn, delay)\"",
|
||||
"To reject a promise after a delay, \"setTimeout(() => reject('err'), delay)\"",
|
||||
"You can resolve and reject when the passed promise resolves or rejects with: \"fn(...params).then(resolve).catch(reject)\""
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "async (n) => { await new Promise(res => setTimeout(res, 100)); return n * n; }\n[5]\n50",
|
||||
"metaData": "{\n \"name\": \"timeLimit\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"inputs\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\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"
|
||||
}
|
||||
}
|
||||
}
|
60
leetcode/originData/sleep.json
Normal file
60
leetcode/originData/sleep.json
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2733",
|
||||
"questionFrontendId": "2621",
|
||||
"boundTopicId": null,
|
||||
"title": "Sleep",
|
||||
"titleSlug": "sleep",
|
||||
"content": "<p>Given a positive integer <code>millis</code>, write an asyncronous function that sleeps for <code>millis</code> milliseconds. It can resolve any value.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> millis = 100\n<strong>Output:</strong> 100\n<strong>Explanation:</strong> It should return a promise that resolves after 100ms.\nlet t = Date.now();\nsleep(100).then(() => {\n console.log(Date.now() - t); // 100\n});\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> millis = 200\n<strong>Output:</strong> 200\n<strong>Explanation:</strong> It should return a promise that resolves after 200ms.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= millis <= 1000</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 48,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Promise Pool\", \"titleSlug\": \"promise-pool\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "100\n200",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number} millis\n */\nasync function sleep(millis) {\n \n}\n\n/** \n * let t = Date.now()\n * sleep(100).then(() => console.log(Date.now() - t)) // 100\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "async function sleep(millis: number): Promise<void> {\n \n}\n\n\n/** \n * let t = Date.now()\n * sleep(100).then(() => console.log(Date.now() - t)) // 100\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"2.3K\", \"totalSubmission\": \"2.6K\", \"totalAcceptedRaw\": 2281, \"totalSubmissionRaw\": 2586, \"acRate\": \"88.2%\"}",
|
||||
"hints": [
|
||||
"In Javascript, you can execute code after some delay with the setTimeout(fn, sleepTime) function.",
|
||||
"An async function is defined as function which returns a Promise.",
|
||||
"To create a Promise, you can code new Promise((resolve, reject) => {}). When you want the function to return a value, code resolve(value) inside the callback."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "100",
|
||||
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"millis\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\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"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode/originData/snail-traversal.json
Normal file
59
leetcode/originData/snail-traversal.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2760",
|
||||
"questionFrontendId": "2624",
|
||||
"boundTopicId": null,
|
||||
"title": "Snail Traversal",
|
||||
"titleSlug": "snail-traversal",
|
||||
"content": "<p>Write code that enhances all arrays such that you can call the <code>snail(rowsCount, colsCount)</code> method that transforms the 1D array into a 2D array organised in the pattern known as <strong>snail traversal order</strong>. Invalid input values should output an empty array. If <code>rowsCount * colsCount !== nums.length</code>, the input is considered invalid.</p>\n\n<p><strong>Snail traversal order</strong><em> </em>starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]<code> </code>with <code>rowsCount = 5</code> and <code>colsCount = 4</code>, the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.</p>\n\n<p> </p>\n\n<p><img alt=\"Traversal Diagram\" src=\"https://assets.leetcode.com/uploads/2023/04/10/screen-shot-2023-04-10-at-100006-pm.png\" style=\"width: 275px; height: 343px;\" /></p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]\nrowsCount = 5\ncolsCount = 4\n<strong>Output:</strong> \n[\n [19,17,16,15],\n [10,1,14,4],\n [3,2,12,20],\n [7,5,18,11],\n [9,8,6,13]\n]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nrowsCount = 1\ncolsCount = 4\n<strong>Output:</strong> [[1, 2, 3, 4]]\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,3]\nrowsCount = 2\ncolsCount = 2\n<strong>Output:</strong> []\n<strong>Explanation:</strong> 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= nums.length <= 250</code></li>\n\t<li><code>1 <= nums[i] <= 1000</code></li>\n\t<li><code>1 <= rowsCount <= 250</code></li>\n\t<li><code>1 <= colsCount <= 250</code></li>\n</ul>\n\n<p> </p>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 9,
|
||||
"dislikes": 10,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Array Prototype Last\", \"titleSlug\": \"array-prototype-last\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]\n5\n4\n[1,2,3,4]\n1\n4\n[1,3]\n2\n2",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number} rowsCount\n * @param {number} colsCount\n * @return {Array<Array<number>>}\n */\nArray.prototype.snail = function(rowsCount, colsCount) {\n\n}\n\n/**\n * const arr = [1,2,3,4];\n * arr.snail(1,4); // [[1,2,3,4]]\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "declare global {\n interface Array<T> {\n snail(rowsCount: number, colsCount: number): number[][];\n }\n}\n\nArray.prototype.snail = function(rowsCount: number, colsCount: number): number[][] {\n\n}\n\n/**\n * const arr = [1,2,3,4];\n * arr.snail(1,4); // [[1,2,3,4]]\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"300\", \"totalSubmission\": \"367\", \"totalAcceptedRaw\": 300, \"totalSubmissionRaw\": 367, \"acRate\": \"81.7%\"}",
|
||||
"hints": [
|
||||
"Different ways to approach this problem. Perhaps store a boolean if you are moving up or down and a current column. Reverse the direction and increment the column every time you hits a wall.",
|
||||
"Is there a way way to do this without storing state - by just using math?"
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]\n5\n4",
|
||||
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"rowsCount\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"colsCount\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"nums\"\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"
|
||||
}
|
||||
}
|
||||
}
|
181
leetcode/originData/sum-of-distances.json
Normal file
181
leetcode/originData/sum-of-distances.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,41 @@
|
||||
<p>Given an integer array <code>arr</code> and a mapping function <code>fn</code>, return a new array with a transformation applied to each element.</p>
|
||||
|
||||
<p>The returned array should be created such that <code>returnedArray[i] = fn(arr[i], i)</code>.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.map</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3], fn = function plusone(n) { return n + 1; }
|
||||
<strong>Output:</strong> [2,3,4]
|
||||
<strong>Explanation:</strong>
|
||||
const newArray = map(arr, plusone); // [2,3,4]
|
||||
The function increases each value in the array by one.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
|
||||
<strong>Output:</strong> [1,3,5]
|
||||
<strong>Explanation:</strong> The function increases each value by the index it resides in.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [10,20,30], fn = function constant() { return 42; }
|
||||
<strong>Output:</strong> [42,42,42]
|
||||
<strong>Explanation:</strong> The function always returns 42.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 1000</code></li>
|
||||
<li><code><font face="monospace">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>
|
||||
<li><font face="monospace"><code>fn returns a number</code></font></li>
|
||||
</ul>
|
25
leetcode/problem/array-prototype-last.html
Normal file
25
leetcode/problem/array-prototype-last.html
Normal file
@ -0,0 +1,25 @@
|
||||
Write code that enhances all arrays such that you can call the <code>array.last()</code> method on any array and it will return the last element. If there are no elements in the array, it should return <code>-1</code>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Calling nums.last() should return the last element: 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = []
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Because there are no elements, return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 1000</code></li>
|
||||
<li><code>0 <= arr[i] <= 1000</code></li>
|
||||
</ul>
|
62
leetcode/problem/array-reduce-transformation.html
Normal file
62
leetcode/problem/array-reduce-transformation.html
Normal file
@ -0,0 +1,62 @@
|
||||
<p>Given an integer array <code>nums</code>, a reducer function <code>fn</code>, and an intial value <code>init</code>, return a <strong>reduced</strong> array.</p>
|
||||
|
||||
<p>A <strong>reduced</strong> array is created by applying the following operation: <code>val = fn(init, nums[0])</code>, <code>val = fn(val, nums[1])</code>, <code>val = fn(val, arr[2])</code>, <code>...</code> until every element in the array has been processed. The final value of <code>val</code> is returned.</p>
|
||||
|
||||
<p>If the length of the array is 0, it should return <code>init</code>.</p>
|
||||
|
||||
<p>Please solve it without using the built-in <code>Array.reduce</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr; }
|
||||
init = 0
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong>
|
||||
initially, the value is init=0.
|
||||
(0) + nums[0] = 1
|
||||
(1) + nums[1] = 3
|
||||
(3) + nums[2] = 6
|
||||
(6) + nums[3] = 10
|
||||
The final answer is 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,2,3,4]
|
||||
fn = function sum(accum, curr) { return accum + curr * curr; }
|
||||
init = 100
|
||||
<strong>Output:</strong> 130
|
||||
<strong>Explanation:</strong>
|
||||
initially, the value is init=100.
|
||||
(100) + nums[0]^2 = 101
|
||||
(101) + nums[1]^2 = 105
|
||||
(105) + nums[2]^2 = 114
|
||||
(114) + nums[3]^2 = 130
|
||||
The final answer is 130.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = []
|
||||
fn = function sum(accum, curr) { return 0; }
|
||||
init = 25
|
||||
<strong>Output:</strong> 25
|
||||
<strong>Explanation:</strong> For empty arrays, the answer is always init.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= init <= 1000</code></li>
|
||||
</ul>
|
56
leetcode/problem/cache-with-time-limit.html
Normal file
56
leetcode/problem/cache-with-time-limit.html
Normal file
@ -0,0 +1,56 @@
|
||||
<p>Write a class that allows getting and setting key-value pairs, however a <strong>time until expiration</strong> is associated with each key.</p>
|
||||
|
||||
<p>The class has three public methods:</p>
|
||||
|
||||
<p><code>set(key, value, duration)</code>: accepts an integer <code>key</code>, an integer <code>value</code>, and a <code>duration</code> in milliseconds. Once the <code>duration</code> has elapsed, the key should be inaccessible. The method should return <code>true</code> if the same un-expired key already exists and <code>false</code> otherwise. Both the value and duration should be overwritten if the key already exists.</p>
|
||||
|
||||
<p><code>get(key)</code>: if an un-expired key exists, it should return the associated value. Otherwise it should return <code>-1</code>.</p>
|
||||
|
||||
<p><code>count()</code>: returns the count of un-expired keys.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
["TimeLimitedCache", "set", "get", "count", "get"]
|
||||
[[], [1, 42, 100], [1], [], [1]]
|
||||
[0, 0, 50, 50, 150]
|
||||
<strong>Output:</strong> [null, false, 42, 1, -1]
|
||||
<strong>Explanation:</strong>
|
||||
At t=0, the cache is constructed.
|
||||
At t=0, a key-value pair (1: 42) is added with a time limit of 100ms. The value doesn't exist so false is returned.
|
||||
At t=50, key=1 is requested and the value of 42 is returned.
|
||||
At t=50, count() is called and there is one active key in the cache.
|
||||
At t=100, key=1 expires.
|
||||
At t=150, get(1) is called but -1 is returned because the cache is empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
["TimeLimitedCache", "set", "set", "get", "get", "get", "count"]
|
||||
[[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []]
|
||||
[0, 0, 40, 50, 120, 200, 250]
|
||||
<strong>Output:</strong> [null, false, true, 50, 50, -1]
|
||||
<strong>Explanation:</strong>
|
||||
At t=0, the cache is constructed.
|
||||
At t=0, a key-value pair (1: 42) is added with a time limit of 50ms. The value doesn't exist so false is returned.
|
||||
At t=40, a key-value pair (1: 50) is added with a time limit of 100ms. A non-expired value already existed so true is returned and the old value was overwritten.
|
||||
At t=50, get(1) is called which returned 50.
|
||||
At t=120, get(1) is called which returned 50.
|
||||
At t=140, key=1 expires.
|
||||
At t=200, get(1) is called but the cache is empty so -1 is returned.
|
||||
At t=250, count() returns 0 because the cache is empty.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= key <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= value <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= duration <= 1000</code></li>
|
||||
<li><code>total method calls will not exceed 100</code></li>
|
||||
</ul>
|
40
leetcode/problem/check-if-object-instance-of-class.html
Normal file
40
leetcode/problem/check-if-object-instance-of-class.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Write a function that checks if a given object is an instance of a given class or superclass.</p>
|
||||
|
||||
<p>There are no constraints on the data types that can be passed to the function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstance(new Date(), Date)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
class Animal {};
|
||||
class Dog extends Animal {};
|
||||
checkIfInstance(new Dog(), Animal); // true
|
||||
|
||||
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstance(Date, Date)
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstance(5, Number)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>5 is a Number. Note that the "instanceof" keyword would return false.
|
||||
</pre>
|
48
leetcode/problem/convert-object-to-json-string.html
Normal file
48
leetcode/problem/convert-object-to-json-string.html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>Given an object, return a valid JSON string of that object. You may assume the object only inludes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by <code>Object.keys()</code>.</p>
|
||||
|
||||
<p>Please solve it without using the built-in <code>JSON.stringify</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = {"y":1,"x":2}
|
||||
<strong>Output:</strong> {"y":1,"x":2}
|
||||
<strong>Explanation:</strong>
|
||||
Return the JSON representation.
|
||||
Note that the order of keys should be the same as the order returned by Object.keys().</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = {"a":"str","b":-12,"c":true,"d":null}
|
||||
<strong>Output:</strong> {"a":"str","b":-12,"c":true,"d":null}
|
||||
<strong>Explanation:</strong>
|
||||
The primitives of JSON are strings, numbers, booleans, and null.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = {"key":{"a":1,"b":[{},null,"Hello"]}}
|
||||
<strong>Output:</strong> {"key":{"a":1,"b":[{},null,"Hello"]}}
|
||||
<strong>Explanation:</strong>
|
||||
Objects and arrays can include other objects and arrays.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> object = true
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
Primitive types are valid inputs.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>object includes strings, integers, booleans, arrays, objects, and null</code></li>
|
||||
<li><code>1 <= JSON.stringify(object).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxNestingLevel <= 1000</code></li>
|
||||
</ul>
|
33
leetcode/problem/counter.html
Normal file
33
leetcode/problem/counter.html
Normal file
@ -0,0 +1,33 @@
|
||||
<p>Given an integer <code>n</code>, return a <code>counter</code> function. This <code>counter</code> function initially returns <code>n</code> and then returns 1 more than the previous value every subsequent time it is called (<code>n</code>, <code>n + 1</code>, <code>n + 2</code>, etc).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
n = 10
|
||||
["call","call","call"]
|
||||
<strong>Output:</strong> [10,11,12]
|
||||
<strong>Explanation:
|
||||
</strong>counter() = 10 // The first time counter() is called, it returns n.
|
||||
counter() = 11 // Returns 1 more than the previous time.
|
||||
counter() = 12 // Returns 1 more than the previous time.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
n = -2
|
||||
["call","call","call","call","call"]
|
||||
<strong>Output:</strong> [-2,-1,0,1,2]
|
||||
<strong>Explanation:</strong> counter() initially returns -2. Then increases after each sebsequent call.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1000<sup> </sup><= n <= 1000</code></li>
|
||||
<li><code>At most 1000 calls to counter() will be made</code></li>
|
||||
</ul>
|
65
leetcode/problem/curry.html
Normal file
65
leetcode/problem/curry.html
Normal file
@ -0,0 +1,65 @@
|
||||
<p>Given a function <code>fn</code>, return a <strong>curried</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>curried</strong> function is a function that accepts fewer or an equal number of parameters as the original function and returns either another <strong>curried</strong> function or the same value the original function would have returned.</p>
|
||||
|
||||
<p>In practical terms, if you called the original function like <code>sum(1,2,3)</code>, you would call the <strong>curried</strong> version like <code>csum(1)(2)(3)<font face="sans-serif, Arial, Verdana, Trebuchet MS">, </font></code><code>csum(1)(2,3)</code>, <code>csum(1,2)(3)</code>, or <code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function should return the same value as the original.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function sum(a, b, c) { return a + b + c; }
|
||||
inputs = [[1],[2],[3]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The code being executed is:
|
||||
const curriedSum = curry(fn);
|
||||
curriedSum(1)(2)(3) === 6;
|
||||
curriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function sum(a, b, c) { return a + b + c; }
|
||||
inputs = [[1,2],[3]]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
curriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function sum(a, b, c) { return a + b + c; }
|
||||
inputs = [[],[],[1,2,3]]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
You should be able to pass the parameters in any way, including all at once or none at all.
|
||||
curriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function life() { return 42; }
|
||||
inputs = [[]]
|
||||
<strong>Output:</strong> 42
|
||||
<strong>Explanation:</strong>
|
||||
currying a function that accepts zero parameters should effectively do nothing.
|
||||
curriedLife() === 42
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= inputs.length <= 1000</code></li>
|
||||
<li><code>0 <= inputs[i][j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= fn.length <= 1000</code></li>
|
||||
<li><code>inputs.flat().length == fn.length</code></li>
|
||||
<li><code>function parameters explicitly defined</code></li>
|
||||
</ul>
|
77
leetcode/problem/debounce.html
Normal file
77
leetcode/problem/debounce.html
Normal file
@ -0,0 +1,77 @@
|
||||
<p>Given a function <code>fn</code> and a time in milliseconds <code>t</code>, return a <strong>debounced</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>debounced</strong> function is a function whose execution is delayed by <code>t</code> milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also recieve the passed parameters.</p>
|
||||
|
||||
<p>For example, let's say <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>60ms</code>, and <code>100ms</code>. The first 2 function calls would be cancelled, and the 3rd function call would be executed at <code>150ms</code>. If instead <code>t = 35ms</code>, The 1st call would be cancelled, the 2nd would be executed at <code>95ms</code>, and the 3rd would be executed at <code>135ms</code>.</p>
|
||||
|
||||
<p><img alt="Debounce Schematic" src="https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-11048-pm.png" style="width: 800px; height: 242px;" /></p>
|
||||
|
||||
<p>The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.debounce()</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 50
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 75, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 125, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
let start = Date.now();
|
||||
function log(...inputs) {
|
||||
console.log([Date.now() - start, inputs ])
|
||||
}
|
||||
const dlog = debounce(log, 50);
|
||||
setTimeout(() => dlog(1), 50);
|
||||
setTimeout(() => dlog(2), 75);
|
||||
|
||||
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
|
||||
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 20
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 100, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed until 70ms. The inputs were (1).
|
||||
The 2nd call is delayed until 120ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 150
|
||||
calls = [
|
||||
{"t": 50, inputs: [1, 2]},
|
||||
{"t": 300, inputs: [3, 4]},
|
||||
{"t": 300, inputs: [5, 6]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
|
||||
The 2nd call is cancelled by the 3rd call
|
||||
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>1 <= calls.length <= 10</code></li>
|
||||
<li><code>0 <= calls[i].t <= 1000</code></li>
|
||||
<li><code>0 <= calls[i].inputs.length <= 10</code></li>
|
||||
</ul>
|
42
leetcode/problem/filter-elements-from-array.html
Normal file
42
leetcode/problem/filter-elements-from-array.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>Given an integer array <code>arr</code> and a filtering function <code>fn</code>, return a new array with a fewer or equal number of elements.</p>
|
||||
|
||||
<p>The returned array should only contain elements where <code>fn(arr[i], i)</code> evaluated to a truthy value.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.filter</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
|
||||
<strong>Output:</strong> [20,30]
|
||||
<strong>Explanation:</strong>
|
||||
const newArray = filter(arr, fn); // [20, 30]
|
||||
The function filters out values that are not greater than 10</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,2,3], fn = function firstIndex(n, i) { return i === 0; }
|
||||
<strong>Output:</strong> [1]
|
||||
<strong>Explanation:</strong>
|
||||
fn can also accept the index of each element
|
||||
In this case, the function removes elements not at index 0
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [-2,-1,0,1,2], fn = function plusOne(n) { return n + 1 }
|
||||
<strong>Output:</strong> [-2,0,1,2]
|
||||
<strong>Explanation:</strong>
|
||||
Falsey values such as 0 should be filtered out
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 1000</code></li>
|
||||
<li><code><font face="monospace">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>
|
||||
</ul>
|
55
leetcode/problem/flatten-deeply-nested-array.html
Normal file
55
leetcode/problem/flatten-deeply-nested-array.html
Normal file
@ -0,0 +1,55 @@
|
||||
<p>Given a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, return a <strong>flattened</strong> version of that array.</p>
|
||||
|
||||
<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>
|
||||
|
||||
<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be <code>0</code>.</p>
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.flat</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 0
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 1
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
The subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 2
|
||||
<strong>Output</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
The maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= count of numbers in arr <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= count of subarrays in arr <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxDepth <= 1000</code></li>
|
||||
<li><code>-1000 <= each number <= 1000</code></li>
|
||||
<li><code><font face="monospace">0 <= n <= 1000</font></code></li>
|
||||
</ul>
|
50
leetcode/problem/function-composition.html
Normal file
50
leetcode/problem/function-composition.html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>Given an array of functions <code>[f<span style="font-size: 10.8333px;">1</span>, f<sub>2</sub>, f<sub>3</sub>, ..., f<sub>n</sub>]</code>, return a new function <code>fn</code> that is the <strong>function composition</strong> of the array of functions.</p>
|
||||
|
||||
<p>The <strong>function composition</strong> of <code>[f(x), g(x), h(x)]</code> is <code>fn(x) = f(g(h(x)))</code>.</p>
|
||||
|
||||
<p>The <strong>function composition</strong> of an empty list of functions is the <strong>identity function</strong> <code>f(x) = x</code>.</p>
|
||||
|
||||
<p>You may assume each function in the array accepts one integer as input and returns one integer as output.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
|
||||
<strong>Output:</strong> 65
|
||||
<strong>Explanation:</strong>
|
||||
Evaluating from right to left ...
|
||||
Starting with x = 4.
|
||||
2 * (4) = 8
|
||||
(8) * (8) = 64
|
||||
(64) + 1 = 65
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
|
||||
<strong>Output:</strong> 1000
|
||||
<strong>Explanation:</strong>
|
||||
Evaluating from right to left ...
|
||||
10 * (1) = 10
|
||||
10 * (10) = 100
|
||||
10 * (100) = 1000
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> functions = [], x = 42
|
||||
<strong>Output:</strong> 42
|
||||
<strong>Explanation:</strong>
|
||||
The composition of zero functions is the identity function</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code><font face="monospace">-1000 <= x <= 1000</font></code></li>
|
||||
<li><code><font face="monospace">0 <= functions.length <= 1000</font></code></li>
|
||||
<li><font face="monospace"><code>all functions accept and return a single integer</code></font></li>
|
||||
</ul>
|
83
leetcode/problem/group-by.html
Normal file
83
leetcode/problem/group-by.html
Normal file
@ -0,0 +1,83 @@
|
||||
<p>Write code that enhances all arrays such that you can call the <code>array.groupBy(fn)</code> method on any array and it will return a <strong>grouped</strong> version of the array.</p>
|
||||
|
||||
<p>A <strong>grouped</strong> array is an object where each key is the output of <code>fn(arr[i])</code> and each value is an array containing all items in the original array with that key.</p>
|
||||
|
||||
<p>The provided callback <code>fn</code> will accept an item in the array and return a string key.</p>
|
||||
|
||||
<p>The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.</p>
|
||||
|
||||
<p>Please solve it without lodash's <code>_.groupBy</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
array = [
|
||||
{"id":"1"},
|
||||
{"id":"1"},
|
||||
{"id":"2"}
|
||||
],
|
||||
fn = function (item) {
|
||||
return item.id;
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"1": [{"id": "1"}, {"id": "1"}],
|
||||
"2": [{"id": "2"}]
|
||||
}
|
||||
<strong>Explanation:</strong>
|
||||
Output is from array.groupBy(fn).
|
||||
The selector function gets the "id" out of each item in the array.
|
||||
There are two objects with an "id" of 1. Both of those objects are put in the first array.
|
||||
There is one object with an "id" of 2. That object is put in the second array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
array = [
|
||||
[1, 2, 3],
|
||||
[1, 3, 5],
|
||||
[1, 5, 9]
|
||||
]
|
||||
fn = function (list) {
|
||||
return String(list[0]);
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
|
||||
}
|
||||
<strong>Explanation:</strong>
|
||||
The array can be of any type. In this case, the selector function defines the key as being the first element in the array.
|
||||
All the arrays have 1 as their first element so they are grouped together.
|
||||
{
|
||||
"1": [[1, 2, 3], [1, 3, 5], [1, 5, 9]]
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
fn = function (n) {
|
||||
return String(n > 5);
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"true": [6, 7, 8, 9, 10],
|
||||
"false": [1, 2, 3, 4, 5]
|
||||
}
|
||||
<strong>Explanation:</strong>
|
||||
The selector function splits the array by whether each number is greater than 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= array.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>fn returns a string</code></li>
|
||||
</ul>
|
48
leetcode/problem/json-deep-equal.html
Normal file
48
leetcode/problem/json-deep-equal.html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>Given two objects <code>o1</code> and <code>o2</code>, check if they are <strong>deeply equal</strong>.</p>
|
||||
|
||||
<p>For two objects to be <strong>deeply equal</strong>, they must contain the same keys, and the associated values must also be <strong>deeply equal</strong>. Two objects are also considered <strong>deeply equal</strong> if they pass the <code>===</code> equality check.</p>
|
||||
|
||||
<p>You may assume both objects are the output of <code>JSON.parse</code>. In other words, they are valid JSON.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.isEqual()</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = {"x":1,"y":2}, o2 = {"x":1,"y":2}
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The keys and values match exactly.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = {"y":2,"x":1}, o2 = {"x":1,"y":2}
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Although the keys are in a different order, they still match exactly.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = {"x":null,"L":[1,2,3]}, o2 = {"x":null,"L":["1","2","3"]}
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The array of numbers is different from the array of strings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> o1 = true, o2 = false
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> true !== false</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= JSON.stringify(o1).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= JSON.stringify(o2).length <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxNestingDepth <= 1000</code></li>
|
||||
</ul>
|
56
leetcode/problem/memoize-ii.html
Normal file
56
leetcode/problem/memoize-ii.html
Normal file
@ -0,0 +1,56 @@
|
||||
<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will return a cached value.</p>
|
||||
|
||||
<p><code>fn</code> can be any function and there are no constraints on what type of values it accepts. Inputs are considered identical if they are <code>===</code> to each other.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
getInputs = () => [[2,2],[2,2],[1,2]]
|
||||
fn = function (a, b) { return a + b; }
|
||||
<strong>Output:</strong> [{"val":4,"calls":1},{"val":4,"calls":1},{"val":3,"calls":2}]
|
||||
<strong>Explanation:</strong>
|
||||
const inputs = getInputs();
|
||||
const memoized = memoize(fn);
|
||||
for (const arr of inputs) {
|
||||
memoized(...arr);
|
||||
}
|
||||
|
||||
For the inputs of (2, 2): 2 + 2 = 4, and it required a call to fn().
|
||||
For the inputs of (2, 2): 2 + 2 = 4, but those inputs were seen before so no call to fn() was required.
|
||||
For the inputs of (1, 2): 1 + 2 = 3, and it required another call to fn() for a total of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
getInputs = () => [[{},{}],[{},{}],[{},{}]]
|
||||
fn = function (a, b) { return ({...a, ...b}); }
|
||||
<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":2},{"val":{},"calls":3}]
|
||||
<strong>Explanation:</strong>
|
||||
Merging two empty objects will always result in an empty object. It may seem like there should only be 1 call to fn() because of cache-hits, however none of those objects are === to each other.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
getInputs = () => { const o = {}; return [[o,o],[o,o],[o,o]]; }
|
||||
fn = function (a, b) { return ({...a, ...b}); }
|
||||
<strong>Output:</strong> [{"val":{},"calls":1},{"val":{},"calls":1},{"val":{},"calls":1}]
|
||||
<strong>Explanation:</strong>
|
||||
Merging two empty objects will always result in an empty object. The 2nd and 3rd third function calls result in a cache-hit. This is because every object passed in is identical.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= inputs.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= inputs.flat().length <= 10<sup>5</sup></code></li>
|
||||
<li><code>inputs[i][j] != NaN</code></li>
|
||||
</ul>
|
80
leetcode/problem/memoize.html
Normal file
80
leetcode/problem/memoize.html
Normal file
@ -0,0 +1,80 @@
|
||||
<p>Given a function <code>fn</code>, return a <strong>memoized</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>memoized </strong>function is a function that will never be called twice with the same inputs. Instead it will returned a cached value.</p>
|
||||
|
||||
<p>You can assume there are <strong>3 </strong>possible input functions: <code>sum</code><strong>, </strong><code>fib</code><strong>, </strong>and <code>factorial</code><strong>.</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>sum</code><strong> </strong>accepts two integers <code>a</code> and <code>b</code> and returns <code>a + b</code>.</li>
|
||||
<li><code>fib</code><strong> </strong>accepts a single integer <code>n</code> and returns <code>1</code> if <font face="monospace"><code>n <= 1</code> </font>or<font face="monospace"> <code>fib(n - 1) + fib(n - 2)</code> </font>otherwise.</li>
|
||||
<li><code>factorial</code> accepts a single integer <code>n</code> and returns <code>1</code> if <code>n <= 1</code> or <code>factorial(n - 1) * n</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
"sum"
|
||||
["call","call","getCallCount","call","getCallCount"]
|
||||
[[2,2],[2,2],[],[1,2],[]]
|
||||
<strong>Output</strong>
|
||||
[4,4,1,3,2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
const sum = (a, b) => a + b;
|
||||
const memoizedSum = memoize(sum);
|
||||
memoizedSum(2, 2); // Returns 4. sum() was called as (2, 2) was not seen before.
|
||||
memoizedSum(2, 2); // Returns 4. However sum() was not called because the same inputs were seen before.
|
||||
// Total call count: 1
|
||||
memoizedSum(1, 2); // Returns 3. sum() was called as (1, 2) was not seen before.
|
||||
// Total call count: 2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input
|
||||
</strong>"factorial"
|
||||
["call","call","call","getCallCount","call","getCallCount"]
|
||||
[[2],[3],[2],[],[3],[]]
|
||||
<strong>Output</strong>
|
||||
[2,6,2,2,6,2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
const factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));
|
||||
const memoFactorial = memoize(factorial);
|
||||
memoFactorial(2); // Returns 2.
|
||||
memoFactorial(3); // Returns 6.
|
||||
memoFactorial(2); // Returns 2. However factorial was not called because 2 was seen before.
|
||||
// Total call count: 2
|
||||
memoFactorial(3); // Returns 6. However factorial was not called because 3 was seen before.
|
||||
// Total call count: 2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input
|
||||
</strong>"fib"
|
||||
["call","getCallCount"]
|
||||
[[5],[]]
|
||||
<strong>Output</strong>
|
||||
[8,1]
|
||||
|
||||
<strong>Explanation
|
||||
</strong>fib(5) = 8
|
||||
// Total call count: 1
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= a, b <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= n <= 10</code></li>
|
||||
<li><code>at most 10<sup>5</sup> function calls</code></li>
|
||||
<li><code>at most 10<sup>5</sup> attempts to access callCount</code></li>
|
||||
<li><code>input function is sum, fib, or factorial</code></li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once amongst the <code>p</code> pairs.</p>
|
||||
|
||||
<p>Note that for a pair of elements at the index <code>i</code> and <code>j</code>, the difference of this pair is <code>|nums[i] - nums[j]|</code>, where <code>|x|</code> represents the <strong>absolute</strong> <strong>value</strong> of <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> <strong>maximum</strong> difference among all </em><code>p</code> <em>pairs.</em> We define the maximum of an empty set to be zero.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,1,2,7,1,3], p = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
|
||||
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,1,2], p = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p>
|
||||
|
||||
<p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p>
|
||||
|
||||
<ul>
|
||||
<li>Cells <code>(i, k)</code> with <code>j < k <= grid[i][j] + j</code> (rightward movement), or</li>
|
||||
<li>Cells <code>(k, j)</code> with <code>i < k <= grid[i][j] + i</code> (downward movement).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>The image above shows one of the paths that visits exactly 3 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,1,0],[1,0,0]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proven that no path exists.
|
||||
</pre>
|
||||
|
||||
<p> </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 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
40
leetcode/problem/prime-in-diagonal.html
Normal file
40
leetcode/problem/prime-in-diagonal.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a 0-indexed two-dimensional integer array <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the largest <strong>prime</strong> number that lies on at least one of the <b>diagonals</b> of </em><code>nums</code>. In case, no prime is present on any of the diagonals, return<em> 0.</em></p>
|
||||
|
||||
<p>Note that:</p>
|
||||
|
||||
<ul>
|
||||
<li>An integer is <strong>prime</strong> if it is greater than <code>1</code> and has no positive integer divisors other than <code>1</code> and itself.</li>
|
||||
<li>An integer <code>val</code> is on one of the <strong>diagonals</strong> of <code>nums</code> if there exists an integer <code>i</code> for which <code>nums[i][i] = val</code> or an <code>i</code> for which <code>nums[i][nums.length - i - 1] = val</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>In the above diagram, one diagonal is <strong>[1,5,9]</strong> and another diagonal is<strong> [3,5,7]</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="font-size: 10.8333px;">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
71
leetcode/problem/promise-pool.html
Normal file
71
leetcode/problem/promise-pool.html
Normal file
@ -0,0 +1,71 @@
|
||||
<p>Given an array of asyncronous functions <code>functions</code> and a <strong>pool limit</strong> <code>n</code>, return an asyncronous function <code>promisePool</code>. It should return a promise that resolves when all the input functions resolve.</p>
|
||||
|
||||
<p><b>Pool limit</b> is defined as the maximum number promises that can be pending at once. <code>promisePool</code> should begin execution of as many functions as possible and continue executing new functions when old promises resolve. <code>promisePool</code> should execute <code>functions[i]</code> then <code>functions[i + 1]</code> then <code>functions[i + 2]</code>, etc. When the last promise resolves, <code>promisePool</code> should also resolve.</p>
|
||||
|
||||
<p>For example, if <code>n = 1</code>, <code>promisePool</code> will execute one function at a time in series. However, if <code>n = 2</code>, it first executes two functions. When either of the two functions resolve, a 3rd function should be executed (if available), and so on until there are no functions left to execute.</p>
|
||||
|
||||
<p>You can assume all <code>functions</code> never reject. It is acceptable for <code>promisePool</code> to return a promise that resolves any value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
functions = [
|
||||
() => new Promise(res => setTimeout(res, 300)),
|
||||
() => new Promise(res => setTimeout(res, 400)),
|
||||
() => new Promise(res => setTimeout(res, 200))
|
||||
]
|
||||
n = 2
|
||||
<strong>Output:</strong> [[300,400,500],500]
|
||||
<strong>Explanation:</strong>
|
||||
Three functions are passed in. They sleep for 300ms, 400ms, and 200ms respectively.
|
||||
At t=0, the first 2 functions are executed. The pool size limit of 2 is reached.
|
||||
At t=300, the 1st function resolves, and the 3rd function is executed. Pool size is 2.
|
||||
At t=400, the 2nd function resolves. There is nothing left to execute. Pool size is 1.
|
||||
At t=500, the 3rd function resolves. Pool size is zero so the returned promise also resolves.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:
|
||||
</strong>functions = [
|
||||
() => new Promise(res => setTimeout(res, 300)),
|
||||
() => new Promise(res => setTimeout(res, 400)),
|
||||
() => new Promise(res => setTimeout(res, 200))
|
||||
]
|
||||
n = 5
|
||||
<strong>Output:</strong> [[300,400,200],400]
|
||||
<strong>Explanation:</strong>
|
||||
At t=0, all 3 functions are executed. The pool limit of 5 is never met.
|
||||
At t=200, the 3rd function resolves. Pool size is 2.
|
||||
At t=300, the 1st function resolved. Pool size is 1.
|
||||
At t=400, the 2nd function resolves. Pool size is 0, so the returned promise also resolves.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
functions = [
|
||||
() => new Promise(res => setTimeout(res, 300)),
|
||||
() => new Promise(res => setTimeout(res, 400)),
|
||||
() => new Promise(res => setTimeout(res, 200))
|
||||
]
|
||||
n = 1
|
||||
<strong>Output:</strong> [[300,700,900],900]
|
||||
<strong>Explanation:</strong>
|
||||
At t=0, the 1st function is executed. Pool size is 1.
|
||||
At t=300, the 1st function resolves and the 2nd function is executed. Pool size is 1.
|
||||
At t=700, the 2nd function resolves and the 3rd function is executed. Pool size is 1.
|
||||
At t=900, the 3rd function resolves. Pool size is 0 so the returned promise resolves.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= functions.length <= 10</code></li>
|
||||
<li><code><font face="monospace">1 <= n <= 10</font></code></li>
|
||||
</ul>
|
71
leetcode/problem/promise-time-limit.html
Normal file
71
leetcode/problem/promise-time-limit.html
Normal file
@ -0,0 +1,71 @@
|
||||
<p>Given an asyncronous function <code>fn</code> and a time <code>t</code> in milliseconds, return a new <strong>time limited</strong> version of the input function.</p>
|
||||
|
||||
<p>A <strong>time limited</strong> function is a function that is identical to the original unless it takes longer than <code>t</code> milliseconds to fullfill. In that case, it will reject with <code>"Time Limit Exceeded"</code>. Note that it should reject with a string, not an <code>Error</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async (n) => {
|
||||
await new Promise(res => setTimeout(res, 100));
|
||||
return n * n;
|
||||
}
|
||||
inputs = [5]
|
||||
t = 50
|
||||
<strong>Output:</strong> {"rejected":"Time Limit Exceeded","time":50}
|
||||
<strong>Explanation:</strong>
|
||||
The provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async (n) => {
|
||||
await new Promise(res => setTimeout(res, 100));
|
||||
return n * n;
|
||||
}
|
||||
inputs = [5]
|
||||
t = 150
|
||||
<strong>Output:</strong> {"resolved":25,"time":100}
|
||||
<strong>Explanation:</strong>
|
||||
The function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async (a, b) => {
|
||||
await new Promise(res => setTimeout(res, 120));
|
||||
return a + b;
|
||||
}
|
||||
inputs = [5,10]
|
||||
t = 150
|
||||
<strong>Output:</strong> {"resolved":15,"time":120}
|
||||
<strong>Explanation:</strong>
|
||||
The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = async () => {
|
||||
throw "Error";
|
||||
}
|
||||
inputs = []
|
||||
t = 1000
|
||||
<strong>Output:</strong> {"rejected":"Error","time":0}
|
||||
<strong>Explanation:</strong>
|
||||
The function immediately throws an error.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= inputs.length <= 10</code></li>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>fn returns a promise</code></li>
|
||||
</ul>
|
29
leetcode/problem/sleep.html
Normal file
29
leetcode/problem/sleep.html
Normal file
@ -0,0 +1,29 @@
|
||||
<p>Given a positive integer <code>millis</code>, write an asyncronous function that sleeps for <code>millis</code> milliseconds. It can resolve any value.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> millis = 100
|
||||
<strong>Output:</strong> 100
|
||||
<strong>Explanation:</strong> It should return a promise that resolves after 100ms.
|
||||
let t = Date.now();
|
||||
sleep(100).then(() => {
|
||||
console.log(Date.now() - t); // 100
|
||||
});
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> millis = 200
|
||||
<strong>Output:</strong> 200
|
||||
<strong>Explanation:</strong> It should return a promise that resolves after 200ms.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= millis <= 1000</code></li>
|
||||
</ul>
|
58
leetcode/problem/snail-traversal.html
Normal file
58
leetcode/problem/snail-traversal.html
Normal file
@ -0,0 +1,58 @@
|
||||
<p>Write code that enhances all arrays such that you can call the <code>snail(rowsCount, colsCount)</code> method that transforms the 1D array into a 2D array organised in the pattern known as <strong>snail traversal order</strong>. Invalid input values should output an empty array. If <code>rowsCount * colsCount !== nums.length</code>, the input is considered invalid.</p>
|
||||
|
||||
<p><strong>Snail traversal order</strong><em> </em>starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]<code> </code>with <code>rowsCount = 5</code> and <code>colsCount = 4</code>, the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><img alt="Traversal Diagram" src="https://assets.leetcode.com/uploads/2023/04/10/screen-shot-2023-04-10-at-100006-pm.png" style="width: 275px; height: 343px;" /></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]
|
||||
rowsCount = 5
|
||||
colsCount = 4
|
||||
<strong>Output:</strong>
|
||||
[
|
||||
[19,17,16,15],
|
||||
[10,1,14,4],
|
||||
[3,2,12,20],
|
||||
[7,5,18,11],
|
||||
[9,8,6,13]
|
||||
]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,2,3,4]
|
||||
rowsCount = 1
|
||||
colsCount = 4
|
||||
<strong>Output:</strong> [[1, 2, 3, 4]]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
nums = [1,3]
|
||||
rowsCount = 2
|
||||
colsCount = 2
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 250</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>1 <= rowsCount <= 250</code></li>
|
||||
<li><code>1 <= colsCount <= 250</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
34
leetcode/problem/sum-of-distances.html
Normal file
34
leetcode/problem/sum-of-distances.html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. There exists an array <code>arr</code> of length <code>nums.length</code>, where <code>arr[i]</code> is the sum of <code>|i - j|</code> over all <code>j</code> such that <code>nums[j] == nums[i]</code> and <code>j != i</code>. If there is no such <code>j</code>, set <code>arr[i]</code> to be <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>arr</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,1,2]
|
||||
<strong>Output:</strong> [5,0,3,4,0]
|
||||
<strong>Explanation:</strong>
|
||||
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
|
||||
When i = 1, arr[1] = 0 because there is no other index with value 3.
|
||||
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
|
||||
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
|
||||
When i = 4, arr[4] = 0 because there is no other index with value 2.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,5,3]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> Since each element in nums is distinct, arr[i] = 0 for all i.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user