mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
67 lines
9.4 KiB
JSON
67 lines
9.4 KiB
JSON
{
|
|
"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 return 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>\nfnName = "sum"\nactions = ["call","call","getCallCount","call","getCallCount"]\nvalues = [[2,2],[2,2],[],[1,2],[]]\n<strong>Output:</strong> [4,4,1,3,2]\n<strong>Explanation:</strong>\nconst sum = (a, b) => a + b;\nconst memoizedSum = memoize(sum);\nmemoizedSum(2, 2); // "call" - returns 4. sum() was called as (2, 2) was not seen before.\nmemoizedSum(2, 2); // "call" - returns 4. However sum() was not called because the same inputs were seen before.\n// "getCallCount" - total call count: 1\nmemoizedSum(1, 2); // "call" - returns 3. sum() was called as (1, 2) was not seen before.\n// "getCallCount" - 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 = "factorial"\nactions = ["call","call","call","getCallCount","call","getCallCount"]\nvalues = [[2],[3],[2],[],[3],[]]\n<strong>Output:</strong> [2,6,2,2,6,2]\n<strong>Explanation:</strong>\nconst factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));\nconst memoFactorial = memoize(factorial);\nmemoFactorial(2); // "call" - returns 2.\nmemoFactorial(3); // "call" - returns 6.\nmemoFactorial(2); // "call" - returns 2. However factorial was not called because 2 was seen before.\n// "getCallCount" - total call count: 2\nmemoFactorial(3); // "call" - returns 6. However factorial was not called because 3 was seen before.\n// "getCallCount" - 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 = "fib"\nactions = ["call","getCallCount"]\nvalues = [[5],[]]\n<strong>Output:</strong> [8,1]\n<strong>Explanation:\n</strong>fib(5) = 8 // "call"\n// "getCallCount" - total call count: 1\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>0 <= actions.length <= 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 "call" and "getCallCount"</li>\n\t<li><code>fnName</code> is one of "sum", "factorial" and "fib"</li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Medium",
|
|
"likes": 477,
|
|
"dislikes": 52,
|
|
"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 * @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 */",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"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 */",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"52.5K\", \"totalSubmission\": \"84.9K\", \"totalAcceptedRaw\": 52481, \"totalSubmissionRaw\": 84870, \"acRate\": \"61.8%\"}",
|
|
"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": {
|
|
"id": "1894",
|
|
"canSeeDetail": false,
|
|
"paidOnly": true,
|
|
"hasVideoSolution": false,
|
|
"paidOnlyVideo": true,
|
|
"__typename": "ArticleNode"
|
|
},
|
|
"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\": \"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}",
|
|
"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 5.1.6, 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 ES2022 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"
|
|
}
|
|
}
|
|
} |