1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/memoize.json

63 lines
13 KiB
JSON
Raw Normal View History

2023-04-23 22:41:08 +08:00
{
"data": {
"question": {
"questionId": "2731",
"questionFrontendId": "2623",
"categoryTitle": "JavaScript",
"boundTopicId": 2222273,
"title": "Memoize",
"titleSlug": "memoize",
2023-12-09 18:42:21 +08:00
"content": "<p>Given a function <code>fn</code>, return a&nbsp;<strong>memoized</strong>&nbsp;version of that function.</p>\n\n<p>A&nbsp;<strong>memoized&nbsp;</strong>function is a function that will never be called twice with&nbsp;the same inputs. Instead it will return&nbsp;a cached value.</p>\n\n<p>You can assume there are&nbsp;<strong>3&nbsp;</strong>possible input functions:&nbsp;<code>sum</code><strong>, </strong><code>fib</code><strong>,&nbsp;</strong>and&nbsp;<code>factorial</code><strong>.</strong></p>\n\n<ul>\n\t<li><code>sum</code><strong>&nbsp;</strong>accepts two integers&nbsp;<code>a</code> and <code>b</code> and returns <code>a + b</code>.</li>\n\t<li><code>fib</code><strong>&nbsp;</strong>accepts a&nbsp;single integer&nbsp;<code>n</code> and&nbsp;returns&nbsp;<code>1</code> if <font face=\"monospace\"><code>n &lt;= 1</code> </font>or<font face=\"monospace\">&nbsp;<code>fib(n - 1) + fib(n - 2)</code>&nbsp;</font>otherwise.</li>\n\t<li><code>factorial</code>&nbsp;accepts a single integer&nbsp;<code>n</code> and returns <code>1</code>&nbsp;if&nbsp;<code>n &lt;= 1</code>&nbsp;or&nbsp;<code>factorial(n - 1) * n</code>&nbsp;otherwise.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfnName = &quot;sum&quot;\nactions = [&quot;call&quot;,&quot;call&quot;,&quot;getCallCount&quot;,&quot;call&quot;,&quot;getCallCount&quot;]\nvalues = [[2,2],[2,2],[],[1,2],[]]\n<strong>Output:</strong> [4,4,1,3,2]\n<strong>Explanation:</strong>\nconst sum = (a, b) =&gt; a + b;\nconst memoizedSum = memoize(sum);\nmemoizedSum(2, 2); // &quot;call&quot; - returns 4. sum() was called as (2, 2) was not seen before.\nmemoizedSum(2, 2); // &quot;call&quot; - returns 4. However sum() was not called because the same inputs were seen before.\n// &quot;getCallCount&quot; - total call count: 1\nmemoizedSum(1, 2); // &quot;call&quot; - returns 3. sum() was called as (1, 2) was not seen before.\n// &quot;getCallCount&quot; - total call count: 2\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:\n</strong>fnName = &quot;factorial&quot;\nactions = [&quot;call&quot;,&quot;call&quot;,&quot;call&quot;,&quot;getCallCount&quot;,&quot;call&quot;,&quot;getCallCount&quot;]\nvalues = [[2],[3],[2],[],[3],[]]\n<strong>Output:</strong> [2,6,2,2,6,2]\n<strong>Explanation:</strong>\nconst factorial = (n) =&gt; (n &lt;= 1) ? 1 : (n * factorial(n - 1));\nconst memoFactorial = memoize(factorial);\nmemoFactorial(2); // &quot;call&quot; - returns 2.\nmemoFactorial(3); // &quot;call&quot; - returns 6.\nmemoFactorial(2); // &quot;call&quot; - returns 2. However factorial was not called because 2 was seen before.\n// &quot;getCallCount&quot; - total call count: 2\nmemoFactorial(3); // &quot;call&quot; - returns 6. However factorial was not called because 3 was seen before.\n// &quot;getCallCount&quot; - total call count: 2\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:\n</strong>fnName = &quot;fib&quot;\nactions = [&quot;call&quot;,&quot;getCallCount&quot;]\nvalues = [[5],[]]\n<strong>Output:</strong> [8,1]\n<strong>Explanation:\n</strong>fib(5) = 8 // &quot;call&quot;\n// &quot;getCallCount&quot; - total call count: 1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= a, b &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= n &lt;= 10</code></li>\n\t<li><code>0 &lt;= actions.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>actions.length === values.length</code></li>\n\t<li><code>actions[i]</code> is one of &quot;call&quot; and &quot;getCallCount&quot;</li>\n\t<li><code>fnName</code> is one of &quot;sum&quot;, &quot;factorial&quot; and&nbsp;&quot;fib&quot;</li>\n</ul>\n",
2023-04-23 22:41:08 +08:00
"translatedTitle": "记忆函数",
2023-12-09 18:42:21 +08:00
"translatedContent": "<p>请你编写一个函数,它接收另一个函数作为输入,并返回该函数的 <strong>记忆化</strong> 后的结果。</p>\n\n<p><strong>记忆函数</strong> 是一个对于相同的输入永远不会被调用两次的函数。相反,它将返回一个缓存值。</p>\n\n<p>你可以假设有 <strong>3</strong> 个可能的输入函数:<code>sum</code> 、<code>fib</code> 和 <code>factorial</code> 。</p>\n\n<ul>\n\t<li>&nbsp;<code>sum</code> 接收两个整型参数 <code>a</code> 和 <code>b</code> ,并返回 <code>a + b</code> 。</li>\n\t<li>&nbsp;<code>fib</code> 接收一个整型参数&nbsp;<code>n</code> ,如果 <code>n &lt;= 1</code> 则返回 <code>1</code>,否则返回 <code>fib (n - 1) + fib (n - 2)</code>。</li>\n\t<li>&nbsp;<code>factorial</code> 接收一个整型参数 <code>n</code> ,如果 <code>n &lt;= 1</code> 则返回&nbsp;&nbsp;<code>1</code>&nbsp;,否则返回 <code>factorial(n - 1) * n</code> 。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfnName = \"sum\"\nactions = [\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\nvalues = [[2,2],[2,2],[],[1,2],[]]\n<strong>输出:</strong>[4,4,1,3,2]\n<strong>解释:</strong>\nconst sum = (a, b) =&gt; a + b;\nconst memoizedSum = memoize(sum);\nmemoizedSum (2, 2);// \"call\" - 返回 4。sum() 被调用,因为之前没有使用参数 (2, 2) 调用过。\nmemoizedSum (2, 2);// \"call\" - 返回 4。没有调用 sum(),因为前面有相同的输入。\n// \"getCallCount\" - 总调用数: 1\nmemoizedSum(1、2);// \"call\" - 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。\n// \"getCallCount\" - 总调用数: 2\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre>\n<strong>输入:\n</strong>fnName = \"factorial\"\nactions = [\"call\",\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\nvalues = [[2],[3],[2],[],[3],[]]\n<strong>输出:</strong>[2,6,2,2,6,2]\n<strong>解释:</strong>\nconst factorial = (n) =&gt; (n &lt;= 1) ? 1 : (n * factorial(n - 1));\nconst memoFactorial = memoize(factorial);\nmemoFactorial(2); // \"call\" - 返回 2。\nmemoFactorial(3); // \"call\" - 返回 6。\nmemoFactorial(2); // \"call\" - 返回 2。 没有调用 factorial(),因为前面有相同的输入。\n// \"getCallCount\" - 总调用数2\nmemoFactorial(3); // \"call\" - 返回 6。 没有调用 factorial(),因为前面有相同的输入。\n// \"getCallCount\" - 总调用数2\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre>\n<strong>输入:\n</strong>fnName = \"fib\"\nactions = [\"call\",\"getCallCount\"]\nvalues = [[5],[]]\n<strong>输出:</strong>[8,1]\n<strong>解释:\n</strong>fib(5) = 8 // \"call\"\n// \"getCallCount\" -&nbsp;总调用数1\n\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= a, b &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= n &lt;= 10</code></li>\n\t<li><code>actions.length === values.length</code></li>\n\t<li><code>actions[i]</code> 为&nbsp;\"call\" 和 \"getCallCount\" 中的一个</li>\n\t<li><code>fnName </code>为 \"sum\", \"factorial\" 和 \"fib\" 中的一个</li>\n</ul>\n",
2023-04-23 22:41:08 +08:00
"isPaidOnly": false,
"difficulty": "Medium",
2023-12-09 18:42:21 +08:00
"likes": 17,
2023-04-23 22:41:08 +08:00
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
2023-12-09 18:42:21 +08:00
"langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}",
2023-04-23 22:41:08 +08:00
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
2023-12-09 18:42:21 +08:00
"code": "/**\n * @param {Function} fn\n * @return {Function}\n */\nfunction memoize(fn) {\n \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 */",
2023-04-23 22:41:08 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
2023-12-09 18:42:21 +08:00
"code": "type Fn = (...params: number[]) => number\n\nfunction memoize(fn: Fn): Fn {\n \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 */",
2023-04-23 22:41:08 +08:00
"__typename": "CodeSnippetNode"
}
],
2023-12-09 19:57:46 +08:00
"stats": "{\"totalAccepted\": \"5.7K\", \"totalSubmission\": \"9.3K\", \"totalAcceptedRaw\": 5699, \"totalSubmissionRaw\": 9305, \"acRate\": \"61.2%\"}",
2023-04-23 22:41:08 +08:00
"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],[]]",
2023-12-09 18:42:21 +08:00
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"fnName\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string[]\",\n \"name\": \"actions\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"values\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
2023-04-23 22:41:08 +08:00
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
2023-12-09 18:42:21 +08:00
"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 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/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>\"]}",
2023-04-23 22:41:08 +08:00
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"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],[]]",
"__typename": "QuestionNode"
}
}
}