1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/interval-cancellation.json
2023-06-12 23:05:37 +08:00

56 lines
8.8 KiB
JSON

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