{ "data": { "question": { "questionId": "2731", "questionFrontendId": "2623", "boundTopicId": null, "title": "Memoize", "titleSlug": "memoize", "content": "

Given a function fn, return a memoized version of that function.

\n\n

memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.

\n\n

You can assume there are possible input functions: sum, fiband factorial.

\n\n\n\n

 

\n

Example 1:

\n\n
\nInput:\nfnName = "sum"\nactions = ["call","call","getCallCount","call","getCallCount"]\nvalues = [[2,2],[2,2],[],[1,2],[]]\nOutput: [4,4,1,3,2]\nExplanation:\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
\n\n

Example 2:

\n\n
\nInput:\nfnName = "factorial"\nactions = ["call","call","call","getCallCount","call","getCallCount"]\nvalues = [[2],[3],[2],[],[3],[]]\nOutput: [2,6,2,2,6,2]\nExplanation:\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
\n\n

Example 3:

\n\n
\nInput:\nfnName = "fib"\nactions = ["call","getCallCount"]\nvalues = [[5],[]]\nOutput: [8,1]\nExplanation:\nfib(5) = 8 // "call"\n// "getCallCount" - total call count: 1\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 478, "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.6K\", \"totalSubmission\": \"85K\", \"totalAcceptedRaw\": 52581, \"totalSubmissionRaw\": 85045, \"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\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }